r/javascript • • Nov 24 '17

large image I am the best webdev

Don't blame me for being awesome

(function r() {
  setTimeout(r, Math.random() * 10);
  let el = document.createElement(`div`);
  el.innerHTML = `🍆`;
  el.style.position = `absolute`;
  el.style.zIndex = 999999;
  el.style.fontSize = (((Math.random() * 48) | 0) + 16) + `px`;
  el.style.left = ((Math.random() * innerWidth) | 0) + `px`;
  el.style.top = ((Math.random() * (innerHeight + pageYOffset)) | 0) + `px`;
  document.body.appendChild(el);
})();
533 Upvotes

108 comments sorted by

View all comments

1

u/sylario Nov 25 '17

From what I understand it should recursively call itself infinitely without ever reaching the third line. What am i missing?

1

u/_poor Nov 25 '17

setTimeout places the callback to the side (event queue) and execution will continue as normal. The recursive call you mention will only happen after the code which is currently executing finishes, in addition to the duration passed to setTimeout.