Skip to content

Instantly share code, notes, and snippets.

@breeze1990
Created March 4, 2017 09:41
Show Gist options
  • Save breeze1990/50b622f45ddf418dc2931abc3c6dd211 to your computer and use it in GitHub Desktop.
Save breeze1990/50b622f45ddf418dc2931abc3c6dd211 to your computer and use it in GitHub Desktop.
window.globalVar = 0;
function eventHandler() { // handler
if (window.globalVar == 0) {
setTimeout(function() { // inside callback
window.globalVar = window.globalVar + 1;
});
}
}
// Intuitively window.globalVar won't exceed 1 no matter how many times the eventHandler is triggered
// But consider the case: the eventHandler is attached to click event and two consecutive clicks happen so fast that
// following sequence of excution could happen:
// handler 1 (if condition eval true)
// -> handler 2 (if condition eval true)
// -> inside callback 1
// -> inside callback 2
// As a result, window.globalVar becomes 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment