Skip to content

Instantly share code, notes, and snippets.

@Arifursdev
Last active January 22, 2025 17:37
Show Gist options
  • Save Arifursdev/f26b9dd1246833da0af3620072c19106 to your computer and use it in GitHub Desktop.
Save Arifursdev/f26b9dd1246833da0af3620072c19106 to your computer and use it in GitHub Desktop.
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