Skip to content

Commit

Permalink
Update what-are-the-built-in-timer-functions.md (#2074)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Yash-Handa authored and Maledong committed Feb 14, 2019
1 parent a0999ec commit fb5a2bf
Showing 1 changed file with 67 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,95 @@ difficulty: 1
layout: knowledge-post.hbs
---

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

setTimeout(function() { console.log("setTimeout: It's been one second!"); }, 1000);
setInterval(function() { console.log("setInterval: It's been one second!"); }, 1000);
```js
setTimeout(function() { console.log("setTimeout: It's been one second!"); }, 1000);
setInterval(function() { console.log("setInterval: It's been one second!"); }, 1000);
```

An example output is:

setTimeout: It's been one second!
setInterval: It's been one second!
setInterval: It's been one second!
setInterval: It's been one second!
setInterval: It's been one second!
...
```bash
setTimeout: It's been one second!
setInterval: It's been one second!
setInterval: It's been one second!
setInterval: It's been one second!
setInterval: It's been one second!
...
```
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.
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:
setInterval(ping, 1000);
```js
setInterval(ping, 1000);
```
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:
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:
var recursive = function () {
console.log("It has been one second!");
setTimeout(recursive,1000);
}
recursive();
```js
const recursive = function () {
console.log("It has been one second!");
setTimeout(recursive,1000);
}
recursive();
```
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.
You can clear the timers you set with `clearTimeout` and `clearInterval`. Their usages are very simple:
function never_call () {
console.log("You should never call this function");
}
```js
function never_call () {
console.log("You should never call this function");
}
var id1 = setTimeout(never_call,1000);
var id2 = setInterval(never_call,1000);
const id1 = setTimeout(never_call,1000);
const id2 = setInterval(never_call,1000);
clearTimeout(id1);
clearInterval(id2);
clearTimeout(id1);
clearInterval(id2);
```
So if you keep track of the return values of the timers, you can easily unhook the timers.
The final trick for the timer objects is you can pass parameters to the callback by passing more parameters to setTimeout and setInterval:
setTimeout(console.log, 1000, "This", "has", 4, "parameters");
setInterval(console.log, 1000, "This only has one");
```js
setTimeout(console.log, 1000, "This", "has", 4, "parameters");
setInterval(console.log, 1000, "This only has one");
```
The output is:
This has 4 parameters
This only has one
This only has one
This only has one
This only has one
This only has one
...
```bash
This has 4 parameters
This only has one
This only has one
This only has one
This only has one
This only has one
...
```
#### setImmediate()
`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:
```js
console.log("This will be printed first");
setImmediate(console.log, "This is an extra parameter");
console.log("This will be printed second");
```
The output is:
```bash
This will be printed first
This will be printed second
This is an extra parameter
```
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 comments on commit fb5a2bf

Please sign in to comment.