diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/solution.md b/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/solution.md index 13f01deb..f0937209 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/solution.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/solution.md @@ -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); +``` diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md index cb9f408e..9fe9ceef 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md @@ -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.