-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvent Queue.html
More file actions
36 lines (29 loc) · 973 Bytes
/
Copy pathEvent Queue.html
File metadata and controls
36 lines (29 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!doctype html>
<html lang="en">
<head>
<title>Event Queue</title>
<meta charset="utf-8">
<script>
//EVENT QUEUE EXAMPLE
//Javascript engine executes the code synchronously line by line.
//However, it adds the asynchronous events like button click event or http event in an EVENT QUEUE.
//These events are executed only after synchronous execution of the documemt.
//Then the engine checks the event queue and executes the events waiting in the queue.
function waitThreeSeconds() {
var ms = 3000 + new Date().getTime();
while (new Date() < ms) {}
console.log('finished function');
}
//In this case click function will be added to Event queue and executes
//only after execution of rest of the code.
function clickHandler() {
console.log('click event ocurred');
}
//listen for the click event on DOM
document.addEventListener('click', clickHandler);
waitThreeSeconds();
console.log("finished execution");
</script>
</head>
<body> </body>
</html>