-
Notifications
You must be signed in to change notification settings - Fork 0
/
047-SetTimeout-Asynchronous.js
55 lines (39 loc) · 1.42 KB
/
047-SetTimeout-Asynchronous.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
author : Jaydatt Patel
setTimeout: The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires. Also it will start to execute in next line even timeout function not executed.
syntax:
setTimeout(functionRef, delay, param1, param2,....., paramN);
param1, …, paramN : Optional Additional arguments which are passed through to the function specified by functionRef.
DOM API is synchronous in nature.
SetTimeout, FileReader and Geolocation are asynchronous in nature and can be used to delay the function, read file and get the location respectively.
Note : It is used with let and var variable with different way.
*/
console.log("------------var----------------");
for (var i = 0; i < 5; i++) {
setTimeout(() => {
console.log(i);
}, 1000);
}
console.log("------------let----------------");
for (let i = 0; i < 5; i++) {
setTimeout(() => {
console.log(i);
}, 1000);
}
console.log("----------------------------");
const myArray = ["zero", "one", "two"];
function printArray(arr, time, obj) {
setTimeout(show, time, arr, time, obj);
}
function show(arr, time, obj) {
if (obj === undefined) {
obj = arr[0];
}
console.log(obj);
if (obj !== arr[arr.length - 1]) {
printArray(arr, time, arr[arr.indexOf(obj) + 1]);
} else {
console.log("Finished");
}
}
printArray(myArray, 1500);