You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In accordance with issue #1977, I have updated the Article how-to-prompt-for-command-line-input with the following changes:
1. Added setImmidiate() with example
2. Some other miscellaneous updates and refactors.
Copy file name to clipboardExpand all lines: locale/en/knowledge/javascript-conventions/what-are-the-built-in-timer-functions.md
+67-32Lines changed: 67 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,60 +9,95 @@ difficulty: 1
9
9
layout: knowledge-post.hbs
10
10
---
11
11
12
-
There are two built-in timer functions, `setTimeout` and `setInterval`, which can be used to call a function at a later time. For an example usage:
12
+
There are two most common built-in timer functions, `setTimeout` and `setInterval`, which can be used to call a function at a later time. For an example usage:
13
13
14
-
setTimeout(function() { console.log("setTimeout: It's been one second!"); }, 1000);
15
-
setInterval(function() { console.log("setInterval: It's been one second!"); }, 1000);
14
+
```js
15
+
setTimeout(function() { console.log("setTimeout: It's been one second!"); }, 1000);
16
+
setInterval(function() { console.log("setInterval: It's been one second!"); }, 1000);
17
+
```
16
18
17
19
An example output is:
18
20
19
-
setTimeout: It's been one second!
20
-
setInterval: It's been one second!
21
-
setInterval: It's been one second!
22
-
setInterval: It's been one second!
23
-
setInterval: It's been one second!
24
-
...
21
+
```bash
22
+
setTimeout: It's been one second!
23
+
setInterval: It's been one second!
24
+
setInterval: It's been one second!
25
+
setInterval: It's been one second!
26
+
setInterval: It's been one second!
27
+
...
28
+
```
25
29
26
30
As you can see the parameters to both are the same. The number second parameter says how long in milliseconds to wait before calling the function passed into the first parameter. The difference between the two functions is that `setTimeout` calls the callback only once while `setInterval` will call it over and over again.
27
31
28
32
Typically you want to be careful with `setInterval` because it can cause some undesirable effects. If, for example, you wanted to make sure your server was up by pinging it every second, you might think to try something like this:
29
33
30
-
setInterval(ping, 1000);
34
+
```js
35
+
setInterval(ping, 1000);
36
+
```
31
37
32
-
This can cause problems, however, if your server is slow and it takes, say, 3 seconds to respond to the first request. In the time it takes to get back the response, you would have sent off 3 more requests - not exactly desirable! This isn't the end of the world when serving small static files, but if you doing an expensive operation such as database query or any complicated computation this can have some undesirable results. A common solution looks like this:
38
+
This can cause problems, however, if your server is slow and it takes, for example, 3 seconds to respond to the first request. In the time it takes to get back the response, you would have sent off 3 more requests - not exactly desirable! Overall, this doesn't have a large impact when serving small static files. Butif you're doing an expensive operation, such as a database query or any complex computation, this can have undesirable results. A common solution looks like this:
33
39
34
-
var recursive = function () {
35
-
console.log("It has been one second!");
36
-
setTimeout(recursive,1000);
37
-
}
38
-
recursive();
40
+
```js
41
+
const recursive = function () {
42
+
console.log("It has been one second!");
43
+
setTimeout(recursive,1000);
44
+
}
45
+
recursive();
46
+
```
39
47
40
48
As you can see, it makes a call to the `recursive` function which, as it completes, makes a call to `setTimeout(recursive, 1000)` which makes it call `recursive` again in 1 second - thus having near the same effect as setInterval while being resilient to the unintended errors that can pile up.
41
49
42
50
You can clear the timers you set with `clearTimeout` and `clearInterval`. Their usages are very simple:
43
51
44
-
function never_call () {
45
-
console.log("You should never call this function");
46
-
}
52
+
```js
53
+
function never_call () {
54
+
console.log("You should never call this function");
55
+
}
47
56
48
-
var id1 = setTimeout(never_call,1000);
49
-
var id2 = setInterval(never_call,1000);
57
+
const id1 = setTimeout(never_call,1000);
58
+
const id2 = setInterval(never_call,1000);
50
59
51
-
clearTimeout(id1);
52
-
clearInterval(id2);
60
+
clearTimeout(id1);
61
+
clearInterval(id2);
62
+
```
53
63
54
64
So if you keep track of the return values of the timers, you can easily unhook the timers.
55
65
56
66
The final trick for the timer objects is you can pass parameters to the callback by passing more parameters to setTimeout and setInterval:
setInterval(console.log, 1000, "This only has one");
71
+
```
60
72
73
+
The output is:
61
74
62
-
This has 4 parameters
63
-
This only has one
64
-
This only has one
65
-
This only has one
66
-
This only has one
67
-
This only has one
68
-
...
75
+
```bash
76
+
This has 4 parameters
77
+
This only has one
78
+
This only has one
79
+
This only has one
80
+
This only has one
81
+
This only has one
82
+
...
83
+
```
84
+
85
+
#### setImmediate()
86
+
87
+
`setImmediate()` is another built-in timer function which as the name suggest, runs immediately after the first iteration of the event loop is completed. In other words, `setImmediate()` is similar to a `setTimeout()` function with a `0ms` delay. The `setImmediate()` function can also take extra parameters that are passed when the callback is called:
88
+
89
+
```js
90
+
console.log("This will be printed first");
91
+
setImmediate(console.log, "This is an extra parameter");
92
+
console.log("This will be printed second");
93
+
```
94
+
95
+
The output is:
96
+
97
+
```bash
98
+
This will be printed first
99
+
This will be printed second
100
+
This is an extra parameter
101
+
```
102
+
103
+
Remember that though `setImmediate()` has no delay (i.e, 0ms) this doesn't mean that the code will run synchronously. It simply means that there will be no delay (i.e, 0ms) after the first iteration of the event loop is completed i.e, all synchronous commands have been executed.
0 commit comments