You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/08-settimeout-setinterval/article.md
+7-7
Original file line number
Diff line number
Diff line change
@@ -158,7 +158,7 @@ The `setTimeout` above schedules next call right at the end of the current one `
158
158
159
159
Recursive `setTimeout` is more flexible method than `setInterval`. This way the next call may be scheduled differently, depending on the results of the current one.
160
160
161
-
For instance, we need to write a service that each 5 seconds sends a request to server asking for data, but in case the server is overloaded, it should increase the interval to 10, 20, 40 seconds...
161
+
For instance, we need to write a service that every 5 seconds sends a request to server asking for data, but in case the server is overloaded, it should increase the interval to 10, 20, 40 seconds...
162
162
163
163
Here's the pseudocode:
164
164
```js
@@ -178,7 +178,7 @@ let timerId = setTimeout(function request() {
178
178
```
179
179
180
180
181
-
And if we regulary have CPU-hungry tasks, then we can measure the time taken by the execution and plan the next call sooner or later.
181
+
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.
182
182
183
183
**Recursive `setTimeout` guarantees a delay between the executions, `setInterval` -- does not.**
184
184
@@ -209,7 +209,7 @@ Did you notice?...
209
209
210
210
**The real delay between `func` calls for `setInterval` is less than in the code!**
211
211
212
-
That's natural, because the time taken by `func` execution "consumes" a part of the interval.
212
+
That's natural, because the time is taken by `func` execution "consumes" a part of the interval.
213
213
214
214
It is possible that `func` execution turns out to be longer than we expected and takes more than 100ms.
For `setInterval` the function stays in memory until `cancelInterval` is called.
237
237
238
-
There's a side-effect. A function references the outer lexical environment, so, while it lives, outer variables live too. They may take much more memory than the function itself. So when we don't need the scheduled function any more, it's better to cancel it, even if it's very small.
238
+
There's a side-effect. A function references the outer lexical environment, so, while it lives, outer variables live too. They may take much more memory than the function itself. So when we don't need the scheduled function anymore, it's better to cancel it, even if it's very small.
239
239
````
240
240
241
241
## setTimeout(...,0)
@@ -260,7 +260,7 @@ The first line "puts the call into calendar after 0ms". But the scheduler will o
260
260
261
261
There's a trick to split CPU-hungry task using `setTimeout`.
262
262
263
-
For instance, syntax highlighting script (used to colorize code examples on this page) is quite CPU-heavy. To hightlight the code, it performs the analysis, creates many colored elements, adds them to the document -- for a big text that takes a lot. It may even cause the browser to "hang", that's unacceptable.
263
+
For instance, syntax highlighting script (used to colorize code examples on this page) is quite CPU-heavy. To highlight the code, it performs the analysis, creates many colored elements, adds them to the document -- for a big text that takes a lot. It may even cause the browser to "hang", that's unacceptable.
264
264
265
265
So we can split the long text to pieces. First 100 lines, then plan another 100 lines using `setTimeout(...,0)`, and so on.
266
266
@@ -276,7 +276,7 @@ let start = Date.now();
276
276
functioncount() {
277
277
278
278
// do a heavy job
279
-
for(let j =0; j <1e9; j++) {
279
+
for(let j =0; j <1e9; j++) {
280
280
i++;
281
281
}
282
282
@@ -401,7 +401,7 @@ Here's the demo:
401
401
let i =0;
402
402
403
403
functioncount() {
404
-
for(let j =0; j <1e6; j++) {
404
+
for(let j =0; j <1e6; j++) {
405
405
i++;
406
406
// put the current i into the <div>
407
407
// (we'll talk more about innerHTML in the specific chapter, should be obvious here)
0 commit comments