Created
July 26, 2022 06:03
-
-
Save wuriyanto48/fe43d7b792a87fcf03d7809e13fee50f to your computer and use it in GitHub Desktop.
Nodejs setInterval usage for delay execution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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