- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
当 alert
等模态对话框窗口打开时,我遇到了 JS setTimeout
的意外行为,我想知道其背后的原因。
我预计 setTimeout(fn,10000) 的意思是“定期检查当前时间,当它大于 Now + 10000 毫秒时触发将调用传递的‘fn’函数的事件处理程序”。这是合乎逻辑的,看看我们如何将超时度量作为“从现在开始的毫秒数”传递。但是,显然,setTimeout
上的倒计时是字面上的倒计时,将在模式窗口打开时暂停。
setTimeout(function(){
//alert A
alert("10 seconds have passed for the first setTimeout")
}, 10000);
setTimeout(function(){
//alert B
alert("Wait for 15 seconds and press OK");
},1000);
我希望在您关闭警报 B 后立即显示警报 A(假设您等待 15 秒),因为警报 A 超时仅 10 秒并且它们已经过去了。然而,实践表明,当警报 B 打开时,警报 A 的倒计时只是暂停,并且它只会在大约 3 分钟后显示。在您关闭警报 B 后又过了 9 秒,无论 B 打开了多长时间。
这似乎不符合逻辑。
更新。 我绝对不是唯一在这里感到困惑的人,因为这种暂停超时的行为发生在 Chrome 和 Internet Explorer 中,而不是 Firefox。 Firefox 执行了我预期的行为——如果你在警报 B 上等待 15 秒——警报 A 会在你关闭它时立即弹出。
最佳答案
对于为什么 IE 和 Chrome 在 alert
被解除之前暂停未决计时器,而 Firefox 没有,我怀疑是否有明确的答案。我相信这只是因为在解释 W3C's specs for alert
时有一定的自由度。 :
The alert(message) method, when invoked, must run the following steps:
If the event loop's termination nesting level is non-zero, optionally abort these steps.
Release the storage mutex.
Show the given message to the user.
Optionally, pause while waiting for for the user to acknowledge the message.
进一步解释第 4 步(暂停)here :
Some of the algorithms in this specification, for historical reasons, require the user agent to pause while running a task until a condition goal is met. This means running the following steps:
If any asynchronously-running algorithms are awaiting a stable state, then run their synchronous section and then resume running their asynchronous algorithm. (See the event loop processing model definition above for details.)
If necessary, update the rendering or user interface of any Document or browsing context to reflect the current state.
Wait until the condition goal is met. While a user agent has a paused task, the corresponding event loop must not run further tasks, and any script in the currently running task must block. User agents should remain responsive to user input while paused, however, albeit in a reduced capacity since the event loop will not be doing anything.
因此,事件循环在任何情况下都会暂停。当警报仍然可见和模式时,不会调用较长超时的回调。如果不是这样,可能会出现各种麻烦事,例如多个警报叠加在一起。
现在,您能从上面的规范中看出计时器倒计时应该在警报的生命周期内暂停,还是应该在警报消失后立即触发?我不能,我什至不确定哪种行为更合乎逻辑。
我敢肯定的是,除调试目的外,您不应该将 JavaScript 警报用于任何其他用途。警报确实允许暂停脚本执行(而某些异步操作如 XHR 正在后台进行),但它们对用户非常不友好。正确的方法是拥抱异步代码,使用 promises 和可能的 ES6 generators/yeild
(如果您追求线性代码风格)。
以下问题高度相关,那里讨论了 alert
的一些替代方法:
关于javascript - 为什么 JS 模态消息框在 setTimeout() 上暂停倒计时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26487403/
我正在学习构建单页应用程序 (SPA) 所需的所有技术。总而言之,我想将我的应用程序实现为单独的层,其中前端仅使用 API Web 服务(json 通过 socket.io)与后端通信。前端基本上是
当我看到存储在我的数据库中的日期时。 这是 正常 。日期和时间就是这样。 但是当我运行 get 请求来获取数据时。 此格式与存储在数据库 中的格式不同。为什么会发生这种情况? 最佳答案 我认为您可以将
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在尝试使用backbone.js 实现一些代码 和 hogan.js (http://twitter.github.com/hogan.js/) Hogan.js was developed ag
我正在使用 Backbone.js、Node.js 和 Express.js 制作一个 Web 应用程序,并且想要添加用户功能(登录、注销、配置文件、显示内容与该用户相关)。我打算使用 Passpor
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我尝试在 NodeJS 中加载数据,然后将其传递给 ExpressJS 以在浏览器中呈现 d3 图表。 我知道我可以通过这种方式加载数据 - https://github.com/mbostock/q
在 node.js 中,我似乎遇到了相同的 3 个文件名来描述应用程序的主要入口点: 使用 express-generator 包时,会创建一个 app.js 文件作为生成应用的主要入口点。 通过 n
最近,我有机会观看了 john papa 关于构建单页应用程序的精彩类(class)。我会喜欢的。它涉及服务器端和客户端应用程序的方方面面。 我更喜欢客户端。在他的实现过程中,papa先生在客户端有类
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是一个图形新手,需要帮助了解各种 javascript 2D 库的功能。 . . 我从 Pixi.js 中得到了什么,而我没有从 Konva 等基于 Canvas 的库中得到什么? 我从 Konva
我正在尝试将一些 LESS 代码(通过 ember-cli-less)构建到 CSS 文件中。 1) https://almsaeedstudio.com/ AdminLTE LESS 文件2) Bo
尝试查看 Express Passport 中所有登录用户的所有 session ,并希望能够查看当前登录的用户。最好和最快的方法是什么? 我在想也许我可以在登录时执行此操作并将用户模型数据库“在线”
我有一个 React 应用程序,但我需要在组件加载完成后运行一些客户端 js。一旦渲染函数完成并加载,运行与 DOM 交互的 js 的最佳方式是什么,例如 $('div').mixItUp() 。对
请告诉我如何使用bodyparser.raw()将文件上传到express.js服务器 客户端 // ... onFilePicked(file) { const url = 'upload/a
我正在尝试从 Grunt 迁移到 Gulp。这个项目在 Grunt 下运行得很好,所以我一定是在 Gulp 中做错了什么。 除脚本外,所有其他任务均有效。我现在厌倦了添加和注释部分。 我不断收到与意外
我正在尝试更改我的网站名称。找不到可以设置标题或应用程序名称的位置。 最佳答案 您可以在 config/ 目录中创建任何文件,例如 config/app.js 包含如下内容: module.expor
经过多年的服务器端 PHP/MySQL 开发,我正在尝试探索用于构建现代 Web 应用程序的新技术。 我正在尝试对所有 JavaScript 内容进行排序,如果我理解得很好,一个有效的解决方案可以是服
我是 Nodejs 的新手。我在 route 目录中有一个 app.js 和一个 index.js。我有一个 app.use(multer....)。我还定义了 app.post('filter-re
我正在使用 angular-seed用于构建我的应用程序的模板。最初,我将所有 JavaScript 代码放入一个文件 main.js。该文件包含我的模块声明、 Controller 、指令、过滤器和
我是一名优秀的程序员,十分优秀!