Created
February 12, 2018 13:08
-
-
Save shreshthmohan/fcacdb711d8c4b60864488f663d575e7 to your computer and use it in GitHub Desktop.
Asynchronous - Callbacks // source http://jsbin.com/midenir
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Asynchronous - Callbacks</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
console.log('Asynchronous - More than one at a time.'); | |
console.log('Try clicking one or more times.'); | |
// noprotect | |
function wait3s() { | |
var ms = 3000 + new Date().getTime(); | |
while (ms > new Date().getTime()) {} | |
console.log('finished 3 second function'); | |
} | |
function clickHandler() { | |
console.log('clicked!'); | |
} | |
document.addEventListener('click', clickHandler); | |
wait3s(); | |
console.log('finished'); | |
console.log('so what\'s happening here? \nHow JS engine executed this is: \n clickHandler function is attached to the click event. So every time user clicks, code inside the clickHandler will be executed.'); | |
console.log('But when will it be executed? Looks like it\'s executed after the last piece of code runs. How does this work?'); | |
console.log('Each time a function is called, an execution context is created. But in the case of things like an event listener, JS engine looks at the event queue only after it has finished everything in it\'s execution stack. So if it sees a click event in the event queue, an instance of execution context is created for clickHander. once that finishes executing, the click event is removed from the event queue. The JS engine will look for another event in the queue. So on.'); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">console.log('Asynchronous - More than one at a time.'); | |
console.log('Try clicking one or more times.'); | |
// noprotect | |
function wait3s() { | |
var ms = 3000 + new Date().getTime(); | |
while (ms > new Date().getTime()) {} | |
console.log('finished 3 second function'); | |
} | |
function clickHandler() { | |
console.log('clicked!'); | |
} | |
document.addEventListener('click', clickHandler); | |
wait3s(); | |
console.log('finished'); | |
console.log('so what\'s happening here? \nHow JS engine executed this is: \n clickHandler function is attached to the click event. So every time user clicks, code inside the clickHandler will be executed.'); | |
console.log('But when will it be executed? Looks like it\'s executed after the last piece of code runs. How does this work?'); | |
console.log('Each time a function is called, an execution context is created. But in the case of things like an event listener, JS engine looks at the event queue only after it has finished everything in it\'s execution stack. So if it sees a click event in the event queue, an instance of execution context is created for clickHander. once that finishes executing, the click event is removed from the event queue. The JS engine will look for another event in the queue. So on.'); | |
</script></body> | |
</html> |
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
console.log('Asynchronous - More than one at a time.'); | |
console.log('Try clicking one or more times.'); | |
// noprotect | |
function wait3s() { | |
var ms = 3000 + new Date().getTime(); | |
while (ms > new Date().getTime()) {} | |
console.log('finished 3 second function'); | |
} | |
function clickHandler() { | |
console.log('clicked!'); | |
} | |
document.addEventListener('click', clickHandler); | |
wait3s(); | |
console.log('finished'); | |
console.log('so what\'s happening here? \nHow JS engine executed this is: \n clickHandler function is attached to the click event. So every time user clicks, code inside the clickHandler will be executed.'); | |
console.log('But when will it be executed? Looks like it\'s executed after the last piece of code runs. How does this work?'); | |
console.log('Each time a function is called, an execution context is created. But in the case of things like an event listener, JS engine looks at the event queue only after it has finished everything in it\'s execution stack. So if it sees a click event in the event queue, an instance of execution context is created for clickHander. once that finishes executing, the click event is removed from the event queue. The JS engine will look for another event in the queue. So on.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment