Skip to content

Instantly share code, notes, and snippets.

@ScriptedAlchemy
Forked from irazasyed/setTimeout-mem-leak.js
Created August 10, 2019 23:05
Show Gist options
  • Save ScriptedAlchemy/2e568be12ed05ab633b685c98e2d8c46 to your computer and use it in GitHub Desktop.
Save ScriptedAlchemy/2e568be12ed05ab633b685c98e2d8c46 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