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.

10

u/[deleted] Jan 03 '21 edited Jan 03 '21

Or just recursion, with an index that's passed into the function.

Something kind of like:

const timeoutList = [1500, 3000, (full list of timeouts)];
function setTimeoutFromArr(idx, cb) {
    if (idx >= timeoutList.length) return;
    setTimeout(() => {
        cb();
        setTimeoutFromArr(idx++, cb);
    }, timeoutList[idx]);
}
setTimeoutFromArr(0, someCbFuncToRerunHere);

Untested, but hopefully you get the idea.

Edit: can't get it to format, just use the automod link I guess. It's my first time writing a code block here from mobile, sorry

1

u/AutoModerator Jan 03 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/Darkcharmer, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

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();

2

u/alulalol Jan 03 '21

Have you heard of async/await?

const sleep = n => new Promise((resolve) => setTimeout(resolve, speed ? 10 : n));

doAction();
await sleep(1000);
doAction();
await sleep(500);
doAction();
await sleep(10000);
doAction();
await sleep(400);
doAction();
await sleep(1000);

1

u/AutoModerator Jan 03 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/alulalol, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/beckettobrien Jan 03 '21

Yeah, I probably could have found a better way to do it looking back so it could be, uh, more readable. It isn’t quite straightforward enough to use a loop because I also have each timeout call a function with a different argument each time, but it probably would be easier to have an array of objects that contain the function to run and the timeout so I could loop over it and have less of a headache, but it’s not for anything important so I don’t care :)