minor
This commit is contained in:
parent
47c2d222bb
commit
49264e23bc
2 changed files with 25 additions and 3 deletions
|
@ -38,5 +38,27 @@ function printNumbers(from, to) {
|
|||
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);
|
||||
```
|
||||
|
|
|
@ -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.**
|
||||
|
||||
|
@ -354,7 +354,7 @@ function 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.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue