0

I've read the following topic is-javascript-guaranteed-to-be-single-threaded There is the good example there:

<textarea id="log" rows="20" cols="40"></textarea>
<input id="inp">
<script type="text/javascript">
    var l= document.getElementById('log');
    var i= document.getElementById('inp');
    i.onblur= function() {
        l.value+= 'blur\n';
    };
    setTimeout(function() {
        l.value+= 'log in\n';
        l.focus();
        l.value+= 'log out\n';
    }, 100);
    i.focus();
</script>

The output is

log in
blur
log out

instead of expected

log in
log out
blur

So this basically shows that my event handler on blur was triggered which interrupted the execution of the code within setTimeout callback. I've read couple of other articles and explanations stating that code will not be interrupted (run-to-completion principle) except for some corner cases like modal popups (alert, prompt windows). So why is it interrupted in the example case?

The only explanation I can come with is that I manually trigger focus event from the code and at the same time the control of the execution is on JS engine. If not triggered manually the blur event during execution of code would be detected by browser and put into the queue which will be processed only after the execution of code is complete (the stack is empty).

Community
  • 1
  • 1
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • 1
    Isn't this explained in the answer you copied the code from? – Barmar Sep 11 '14 at 09:48
  • Well, the problem with the answer there is that the author mixes this example with modal popups `These events don't just fire because you called focus() directly, they could happen because you called alert(), or opened a pop-up window, or anything else that moves the focus.`. And then most of his answer is concerned with modal popups. Yet, the first example and modal popups are very different. – Max Koretskyi Sep 11 '14 at 09:53
  • 1
    He explained it right above the code: _Browsers will fire these right away when your code does something to cause them:_ – Barmar Sep 11 '14 at 09:54
  • Thanks, so judging by [this answer](http://stackoverflow.com/a/21239511/2545680) I can assume that the fact that my code is interrupted is a bug? – Max Koretskyi Sep 11 '14 at 09:59

1 Answers1

1

As stated in the question you linked to:

Browsers will fire these [events] right away when your code does something to cause them:

In the above example, focusing on an element causes an immediate blur event on the element that previously had the focus. This event isn't queued, it's triggered immediately. In effect, the focus() method calls the blur listeners for the previously-focused element directly.

It's useful to do this immediately so that the blur handlers can be sure to have access to the current state of the element being left; we don't want to give other code a chance to change the field first.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks! But what about the answer I mentioned which dismisses the notion of immediate events, i.e. events not added to queue? Do you know any other source to read about events that are not added to queue? – Max Koretskyi Sep 11 '14 at 10:17
  • I posted a question about it in the comments there. If you have questions about an answer, that's what you should do, not post a new question. – Barmar Sep 11 '14 at 10:19
  • I see, thanks. I just thought that discussing stuff in comments is not a good thing. I'll be following your comment there. – Max Koretskyi Sep 11 '14 at 10:26
  • I think I've found the answer. These are called `Synchronous events` and explained pretty detailed [here](http://javascript.info/tutorial/events-and-timing-depth). You may want to edit your answer a bit if find the information in the link relevant. – Max Koretskyi Sep 11 '14 at 10:46
  • I just didn't think it would be fair since I've already accepted your answer :) – Max Koretskyi Sep 11 '14 at 10:53
  • @Maximus You should create an answer from your comment. – AndreKR Aug 06 '15 at 16:57
  • @Maximus If your answer is more complete than mine, feel free to post it and unaccept mine. I'll even upvote it. – Barmar Aug 06 '15 at 18:19