有什么东西作为“即时事件”在Javascript中? [英] Is there something as "immediate events" in Javascript?

查看:182
本文介绍了有什么东西作为“即时事件”在Javascript中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时事件的概念/概念是否存在于Javascript实现中?

Is the notion/concept of "immediate events" something that exists in Javascript implementations?

在此此回答到问题保证JavaScript是单线程的?auther提到他指的是即时事件。这样的立即事件是一个回调函数(即,像浏览器窗口的resize window.onresize = callbackfunc; ),在某些其他Javascript代码尚未完成的情况下执行,因为对于instace它被阻止(即 alert(alert); )。

In this this answer to the question "Is javascript guaranteed to be single-threaded?" the auther mentions something he refers to as immediate events. Such an immediate event is a callback function (i.e. to something like the "resize" of the browser window window.onresize = callbackfunc; ), which is executed while some other Javascript code has not yet "finished", because for instace it is blocked (i.e. alert("alert");).

让我以一个例子说明:

// (1) Setup a "immediate event" (a callback for "resize");
window.onresize = function(){ console.log("log resize"); };

// (2) Run some code which contains a blocking alert()
console.log("log A");
console.log("log B");
alert("alert");
console.log("log C");

(as a jsfiddle http://jsfiddle.net/rj25m/5/

(As a jsfiddle http://jsfiddle.net/rj25m/5/)

上述脚本的输出用于不同的情况/场景:

Outputs of the script above for different cases/scenario:

案例1(不调整大小)


log A
log B
log C

情况2(Windows在 alert(alert)后调整大小;

Case 2 (Windows is resized after alert("alert");)


log A
log B
log C
log resize

案例3 有趣的案例(在警报); popup)

Case 3 interesting case (Windows is resized during the alert("alert"); popup)


log A
log B
log resize
log C

注意:请注意(如 answer 提到),在某些系统上是不容易调整窗口大小的模态警报期间。在MS Windows中,例如,一个需要临时减少屏幕旋转,引起窗口的大小调整。在其他系统中,大多数linux,事件虽然有一个警报,你仍然可以调整窗口大小。

NOTICE: Please observe (as mentioned also in the answer mentioned), that on some systems it is not easy to resize the window during there is a modal alert. In MS Windows for instance one needs to temparily reduced the screen revolution to provoke the resize of the window. In other system, most linux, event though there is a alert you can still resize the window.

注意2:(情况3)输出我只能在Firefox中重现(使用26.0版本)。 IE和Chrome似乎并没有支持这些即时事件,似乎有resize事件被注意到,但计划运行完成后阻止的 alert(alert)

NOTICE 2: The (Case 3) output I was only able to reproduce in Firefox (used the 26.0 version). IE and Chrome did not seem to "support" those "immediate events" and it seems that there the resize event was noticed but scheduled to run after the completion of the block which was blocked by the alert("alert").

案例4(类似Case3,但使用Chrome / IE)

Case 4 (like Case3, but with Chrome/IE)


log A
log B
log C
log resize

Firefox构建和术语即时事件的使用。我有我的怀疑,如果这样的事情实际存在。这就是为什么我在这里问,我期望有人是熟悉的,可以回答参考ECMAScript的规范和或参考的实现规范。
查看MDN对运行到完成
我倾向于认为即时事件描述的行为是不需要的,没有命名和实际的火狐错误。有一些名声firefox有一些麻烦与警报,因为他们介绍了新的(标签模式警报)。

I am confused about the behaviour observed in the Firefox build and the usage of the term "immediate events". I have my doubts if something like this actually exists. This is why I ask here, I expect that somebody is familiar and can answer with reference to the specs of ECMAScript and or reference to the specs of implementations. Looking at the MDN reference to the actual idea of "run to completion" I am inclined to think that the behaviour descriped by immediate events is nothing desired, nothing named and actuallye a Firefox bug. There is some reputation that firefox has some troubles with the alert since they introduced the new (tab modal alert).

推荐答案

Javascript中没有即时事件将永远不会被中断处理事件。

There is no thing as "immediate events" in Javascript, normally code will never be interrupted to handle events.

具有模态弹出窗口(如alert)的情况是唯一的例外。在某些浏览器中,当您调用警报时,方法中的代码可以设置为保持,并且在警报打开时发生事件。通常,在警报打开时,没有需要处理的事件,但显然您已经发现异常。

The case with modal popups like alert is the only exception. In some browsers the code in a method can be put "on hold" when you call alert, and there are events happening while the alert is open. Normally there would be no events that needed handling while the alert is open, but obviously you have found an exception.

通常,运行到完成原则是规则。除非您使用 alert 提示符,否则您可以依赖运行的代码不中断,直到退出函数。在代码运行时,浏览器实际上没有什么发生;没有页面更新,甚至GIF动画移动,而有Javascript运行。

Normally the "run-to-completion" principle is the rule. Unless you use something like alert or prompt, you can rely on the code being run uninterrupted until you exit your function. Actually nothing happens in the browser while the code is running; there are no page updates, not even GIF animations move while there is Javascript running.

这篇关于有什么东西作为“即时事件”在Javascript中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆