Skip to content

Instantly share code, notes, and snippets.

@denizozger
Created August 5, 2016 12:07
Show Gist options
  • Save denizozger/e40a58fc1a13cbe453ea3df188086c36 to your computer and use it in GitHub Desktop.
Save denizozger/e40a58fc1a13cbe453ea3df188086c36 to your computer and use it in GitHub Desktop.
Visualise settimeout behaviour in call stack
function log(n) { console.log(n); }
setTimeout(function A() {
setTimeout(function B() {
log(1);
setTimeout(function D() { log(2); }, 0);
setTimeout(function E() { log(3); }, 0);
}, 0);
setTimeout(function C() {
log(4);
setTimeout(function F() { log(5); }, 0);
setTimeout(function G() { log(6); }, 0);
}, 0);
}, 0);
http://latentflip.com/loupe/?code=CmZ1bmN0aW9uIGxvZyhuKSB7IGNvbnNvbGUubG9nKG4pOyB9CgpzZXRUaW1lb3V0KGZ1bmN0aW9uIEEoKSB7CiAgc2V0VGltZW91dChmdW5jdGlvbiBCKCkgewogICAgbG9nKDEpOwogICAgc2V0VGltZW91dChmdW5jdGlvbiBEKCkgeyBsb2coMik7IH0sIDApOwogICAgc2V0VGltZW91dChmdW5jdGlvbiBFKCkgeyBsb2coMyk7IH0sIDApOwogIH0sIDApOwogIHNldFRpbWVvdXQoZnVuY3Rpb24gQygpIHsKICAgIGxvZyg0KTsKICAgIHNldFRpbWVvdXQoZnVuY3Rpb24gRigpIHsgbG9nKDUpOyB9LCAwKTsKICAgIHNldFRpbWVvdXQoZnVuY3Rpb24gRygpIHsgbG9nKDYpOyB9LCAwKTsKICB9LCAwKTsKfSwgMCk7!!!PGJ1dHRvbj5DbGljayBtZSE8L2J1dHRvbj4%3D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment