This commit is contained in:
Ilya Kantor 2019-05-19 22:07:01 +03:00
parent 47c2d222bb
commit 49264e23bc
2 changed files with 25 additions and 3 deletions

View file

@ -38,5 +38,27 @@ function printNumbers(from, to) {
printNumbers(5, 10); printNumbers(5, 10);
``` ```
Note that in both solutions, there is an initial delay before the first output. Sometimes we need to add a line to make the first output immediately, that's easy to do. Note that in both solutions, there is an initial delay before the first output. The function is called after `1000ms` the first time.
If we also want the function to run immediately, then we can add an additional call on a separate line, like this:
```js run
function printNumbers(from, to) {
let current = from;
function go() {
alert(current);
if (current == to) {
clearInterval(timerId);
}
current++;
}
*!*
go();
*/!*
let timerId = setInterval(go, 1000);
}
printNumbers(5, 10);
```

View file

@ -176,7 +176,7 @@ let timerId = setTimeout(function request() {
``` ```
And if we regularly have CPU-hungry tasks, then we can measure the time taken by the execution and plan the next call sooner or later. And if we the functions that we're scheduling are CPU-hungry, then we can measure the time taken by the execution and plan the next call sooner or later.
**Recursive `setTimeout` guarantees a delay between the executions, `setInterval` -- does not.** **Recursive `setTimeout` guarantees a delay between the executions, `setInterval` -- does not.**
@ -354,7 +354,7 @@ function count() {
count(); count();
``` ```
Now when we start to `count()` and know that we'll need to `count()` more, we schedule that immediately, before doing the job. Now when we start to `count()` and see that we'll need to `count()` more, we schedule that immediately, before doing the job.
If you run it, it's easy to notice that it takes significantly less time. If you run it, it's easy to notice that it takes significantly less time.