Skip to content

Commit fb5a2bf

Browse files
Yash-HandaMaledong
authored andcommitted
Update what-are-the-built-in-timer-functions.md (#2074)
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.
1 parent a0999ec commit fb5a2bf

File tree

1 file changed

+67
-32
lines changed

1 file changed

+67
-32
lines changed

locale/en/knowledge/javascript-conventions/what-are-the-built-in-timer-functions.md

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,95 @@ difficulty: 1
99
layout: knowledge-post.hbs
1010
---
1111

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:
1313

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+
```
1618

1719
An example output is:
1820

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+
```
2529
2630
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.
2731
2832
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:
2933
30-
setInterval(ping, 1000);
34+
```js
35+
setInterval(ping, 1000);
36+
```
3137
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. But if 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:
3339
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+
```
3947
4048
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.
4149
4250
You can clear the timers you set with `clearTimeout` and `clearInterval`. Their usages are very simple:
4351
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+
}
4756
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);
5059
51-
clearTimeout(id1);
52-
clearInterval(id2);
60+
clearTimeout(id1);
61+
clearInterval(id2);
62+
```
5363
5464
So if you keep track of the return values of the timers, you can easily unhook the timers.
5565
5666
The final trick for the timer objects is you can pass parameters to the callback by passing more parameters to setTimeout and setInterval:
5767
58-
setTimeout(console.log, 1000, "This", "has", 4, "parameters");
59-
setInterval(console.log, 1000, "This only has one");
68+
```js
69+
setTimeout(console.log, 1000, "This", "has", 4, "parameters");
70+
setInterval(console.log, 1000, "This only has one");
71+
```
6072
73+
The output is:
6174
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

Comments
 (0)