Javascript event loop model

based on: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/EventLoop

the stack stack is empty before processing the next event. So why in the next snippet alert it displays 1 instead of 0, because the alert function must be run before the callback

var a=0; var b={}; $(b).on("event", function (){ a++; }); $(b).trigger("event"); alert(a); 

http://jsfiddle.net/nxjhokL0/

Thanks!

+7
javascript
source share
1 answer

Let's ignore the fact that there are jQuery events here, not native DOM events, as this is reproduced using native DOM events, as dystroy showed in a comment on the question.

Simply put, MDN is misleading. In general, this article may use a technical review.

If we check the DOM Events specification :

Events can be sent synchronously or asynchronously.

"the stack stack is empty before the next event is processed." Generally incorrect. This only happens with asynchronous events.

+3
source share

All Articles