Skip to content

Instantly share code, notes, and snippets.

@zeusdeux
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeusdeux/4e78ec74ca11b1ff236c to your computer and use it in GitHub Desktop.
Save zeusdeux/4e78ec74ca11b1ff236c to your computer and use it in GitHub Desktop.
Event loop peculiarities
(function(){
var dudeEvent = new CustomEvent('dude');
var body = document.querySelector('body');
body.addEventListener('dude', function(){
console.log('dude listener');
console.log('alerting')
alert('wat');
});
function a(){
body.dispatchEvent(dudeEvent);
console.log('still in a');
}
a();
function b(){
setTimeout(body.dispatchEvent.bind(body, dudeEvent),0);
console.log('still in b');
}
b();
})();
//output in chrome/firefox
//"dude listener"
//"alerting"
//"still in a"
//"still in b"
//"dude listener"
//"alerting"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment