Last active
January 22, 2025 17:37
-
-
Save Arifursdev/f26b9dd1246833da0af3620072c19106 to your computer and use it in GitHub Desktop.
This file contains 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
const repeatWithTimeout = (action, interval) => { | |
let timerId; | |
function repeat() { | |
action(); | |
timerId = setTimeout(repeat, interval); | |
} | |
repeat(); | |
return () => { | |
clearTimeout(timerId); | |
}; | |
}; | |
// Example usage | |
const cancel = repeatWithTimeout(() => { | |
console.log("Action executed"); | |
}, 1000); | |
// Cancel the repeating action after 5 seconds | |
setTimeout(() => { | |
cancel(); | |
console.log("Repeating action canceled"); | |
}, 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment