Skip to content

Instantly share code, notes, and snippets.

@irazasyed
Last active November 14, 2023 23:43
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save irazasyed/305594865c4b4d0b6716 to your computer and use it in GitHub Desktop.
Save irazasyed/305594865c4b4d0b6716 to your computer and use it in GitHub Desktop.
JavaScript: setTimeout Memory Leak Prevention.
// https://developer.mozilla.org/en-US/docs/Web/API/window.setTimeout
var timeoutID;
delayedAlert();
function delayedAlert() {
timeoutID = window.setTimeout(slowAlert, 2000);
}
function slowAlert() {
alert("That was really slow!");
clearAlert();
}
function clearAlert() {
window.clearTimeout(timeoutID);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment