Skip to content

Instantly share code, notes, and snippets.

@wuriyanto48
Created July 26, 2022 06:03
Show Gist options
  • Save wuriyanto48/fe43d7b792a87fcf03d7809e13fee50f to your computer and use it in GitHub Desktop.
Save wuriyanto48/fe43d7b792a87fcf03d7809e13fee50f to your computer and use it in GitHub Desktop.
Nodejs setInterval usage for delay execution
let sendStatus = 'FAILED';
// DELAY in Milliseconds
const DELAY = 1000; // 1 second
const retryEmail = () => {
console.log('retry failed email');
return 'FAILED';
};
if (sendStatus === 'FAILED') {
let intervalId;
let counter = 1;
if (!intervalId) {
intervalId = setInterval(() => {
const reply = retryEmail();
if (reply === 'SUCCEED' || counter === 3) {
clearInterval(intervalId);
intervalId = null;
}
counter = counter + 1;
}, DELAY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment