-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimers.js
More file actions
47 lines (33 loc) · 853 Bytes
/
timers.js
File metadata and controls
47 lines (33 loc) · 853 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
37
38
39
40
41
42
43
44
45
46
47
// count with setTimeout used recursively
const doThing1 = () => {
console.log(++counter1)
if(counter1 < 5) {
setTimeout(doThing1, 1000)
} else {
console.log("done")
}
}
let counter1 = 0
doThing1()
// count with setInterval and clearInterval
const doThing2 = () => {
console.log(++counter2)
if(counter2 >= 5) {
clearInterval(timer2)
console.log("done")
}
}
let counter2 = 0
timer2 = setInterval(doThing2, 1000)
// count with setInterval and stop at time
const doThing3 = () => {
console.log(++counter3)
if(Date.now() >= timeStart + 5 * 1000) {
clearInterval(timer3)
console.log("finished:", new Date(Date.now()).toDateString())
}
}
let timeStart = Date.now()
console.log(timeStart)
let counter3 = 0
let timer3 = setInterval(doThing3, 1000)