-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetTimeOut.js
49 lines (38 loc) · 1.06 KB
/
setTimeOut.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
/* Set Timeout */
console.log("Blocking code which is executed first and then moves to next line")
setTimeout(() => { //non-blocking code which is added into queue
console.log("Takes 2 sec to show the console");
}, 2000);
/* Clear Timeout */
async function scheduledatafetching(){
let fetchdata;
fetchdata = setTimeout(() => {
console.log("Clear timeout");
}, 2000);
//logic if logic fails we can stop the timeout that has been added in queue by using clearTimeout
let apicall = true;
if(apicall == false)
{
clearTimeout(fetchdata);
}
}
scheduledatafetching();
/* Set Interval */
function getValue(){
return new Promise((resolve,reject)=>{
setTimeout(() => {
return resolve(true);
}, 2000);
})
}
const id = setInterval(getStatus,2000); //executes the function getstatus() until clearInterval() is called
async function getStatus(){
let status = await getValue();
if(status==true)
{
console.log("completed");
clearInterval(id);
}
}
getStatus();
/* Clear Interval */