r/badcode • • Jan 03 '21

js setTimeoutception

Post image
719 Upvotes

35 comments sorted by

View all comments

23

u/[deleted] Jan 03 '21

If only there was a way to... loop

12

u/xigoi Jan 03 '21

It's not that easy with setTimeout. You'll need to do some recursive magic.

5

u/Somepotato Jan 03 '21

async/await, multiple outer level timeouts, or callback reuse, done

5

u/[deleted] Jan 03 '21
function delay(ms) {
  return new Promise(function (resolve) {
    setTimeout(resolve, ms);
  });
}

... await delay(1000); doThing(); await delay (500); doOtherThing();