Created
March 4, 2017 09:41
-
-
Save breeze1990/50b622f45ddf418dc2931abc3c6dd211 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
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