Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

concurrency - Is there something as "immediate events" in Javascript?

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

Background

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");).

Let me clarify with an example:

// (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/)

Outputs of the script above for different cases/scenario:

Case 1 (No resizing takes place)

log A
log B
log C

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

log A
log B
log C
log resize

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

log A
log B
log resize
log C

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.

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").

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

log A
log B
log C
log resize

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).

I am happy for insight and open to answer comments

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...