Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 49264e2

Browse files
committedMay 19, 2019
minor
1 parent 47c2d22 commit 49264e2

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed
 

‎1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/solution.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,27 @@ function printNumbers(from, to) {
3838
printNumbers(5, 10);
3939
```
4040

41-
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.
41+
Note that in both solutions, there is an initial delay before the first output. The function is called after `1000ms` the first time.
4242

43+
If we also want the function to run immediately, then we can add an additional call on a separate line, like this:
44+
45+
```js run
46+
function printNumbers(from, to) {
47+
let current = from;
48+
49+
function go() {
50+
alert(current);
51+
if (current == to) {
52+
clearInterval(timerId);
53+
}
54+
current++;
55+
}
56+
57+
*!*
58+
go();
59+
*/!*
60+
let timerId = setInterval(go, 1000);
61+
}
62+
63+
printNumbers(5, 10);
64+
```

‎1-js/06-advanced-functions/08-settimeout-setinterval/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ let timerId = setTimeout(function request() {
176176
```
177177

178178

179-
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.
179+
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.
180180

181181
**Recursive `setTimeout` guarantees a delay between the executions, `setInterval` -- does not.**
182182

@@ -354,7 +354,7 @@ function count() {
354354
count();
355355
```
356356

357-
Now when we start to `count()` and know that we'll need to `count()` more, we schedule that immediately, before doing the job.
357+
Now when we start to `count()` and see that we'll need to `count()` more, we schedule that immediately, before doing the job.
358358

359359
If you run it, it's easy to notice that it takes significantly less time.
360360

0 commit comments

Comments
 (0)
Please sign in to comment.