-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimers.js
42 lines (36 loc) · 1.41 KB
/
timers.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
// To run this script: node timers
// When writing JavaScript code, you might want to delay the execution of a function
// This is the job of setTimeout, we give a callback function and a delay value(in milliseconds)
setTimeout(() => {
console.log("I executed after 2 seconds");
}, 2000);
// Calling setTimeout with a function which has some parameters
setTimeout(
(name, age) => {
console.log(`My name is ${name} and I am ${age} years old`);
},
5000,
"Fatih", // name parameter
30 // age parameter
);
// If you specify the timeout delay to 0, the callback function will be executed as soon as possible, but after the current
// function execution
// This is especially useful to avoid blocking the CPU on intensive tasks and let other functions be executed while performing
// a heavy calculation, by queuing functions in the scheduler.
setTimeout(() => {
console.log(
"I will execute as soon as possible because of 0 milliseconds delay"
);
}, 0);
console.log(
"I will execute first because all other functions are in setTimeout"
);
// setInterval will run the function forever, at the specific time interval you specify (in milliseconds)
let intervalID = setInterval(() => {
console.log("I will appear in every 6 seconds");
}, 6000);
// Let's clear the interval after 30 seconds by using setTimeout and clearInterval
setTimeout(() => {
clearInterval(intervalID);
console.log("Interval Cleared");
}, 30000);