Skip to content

Commit a5e3043

Browse files
authored
Update article.md
1 parent ccc0e93 commit a5e3043

File tree

1 file changed

+7
-7
lines changed
  • 1-js/06-advanced-functions/08-settimeout-setinterval

1 file changed

+7
-7
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ The `setTimeout` above schedules next call right at the end of the current one `
158158

159159
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.
160160

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...
162162

163163
Here's the pseudocode:
164164
```js
@@ -178,7 +178,7 @@ let timerId = setTimeout(function request() {
178178
```
179179

180180

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.
182182

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

@@ -209,7 +209,7 @@ Did you notice?...
209209

210210
**The real delay between `func` calls for `setInterval` is less than in the code!**
211211

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.
213213

214214
It is possible that `func` execution turns out to be longer than we expected and takes more than 100ms.
215215

@@ -235,7 +235,7 @@ setTimeout(function() {...}, 100);
235235
236236
For `setInterval` the function stays in memory until `cancelInterval` is called.
237237
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.
239239
````
240240

241241
## setTimeout(...,0)
@@ -260,7 +260,7 @@ The first line "puts the call into calendar after 0ms". But the scheduler will o
260260

261261
There's a trick to split CPU-hungry task using `setTimeout`.
262262

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.
264264

265265
So we can split the long text to pieces. First 100 lines, then plan another 100 lines using `setTimeout(...,0)`, and so on.
266266

@@ -276,7 +276,7 @@ let start = Date.now();
276276
function count() {
277277

278278
// do a heavy job
279-
for(let j = 0; j < 1e9; j++) {
279+
for (let j = 0; j < 1e9; j++) {
280280
i++;
281281
}
282282

@@ -401,7 +401,7 @@ Here's the demo:
401401
let i = 0;
402402
403403
function count() {
404-
for(let j = 0; j < 1e6; j++) {
404+
for (let j = 0; j < 1e6; j++) {
405405
i++;
406406
// put the current i into the <div>
407407
// (we'll talk more about innerHTML in the specific chapter, should be obvious here)

0 commit comments

Comments
 (0)