Skip to content

Instantly share code, notes, and snippets.

@d0peCode
Last active August 17, 2019 19:50
Show Gist options
  • Save d0peCode/708016b96965cbf82cc81c96d0fed85c to your computer and use it in GitHub Desktop.
Save d0peCode/708016b96965cbf82cc81c96d0fed85c to your computer and use it in GitHub Desktop.
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
setTimeout(() => console.log('1200ms elapsed'), 1200);
const foo = () => {
let intervalId;
const intervalFunction = async() => {
clearInterval(intervalId); //clear so it will not execute again after 100ms
console.log('interval executed');
await sleep(120); //some long action
return intervalId = setInterval(intervalFunction, 100); //reassign id and start after sleep()
};
setInterval(intervalFunction, 100);
};
foo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment