This commit is contained in:
Ilya Kantor 2019-05-19 23:05:25 +03:00
parent b52dd6fd7b
commit 5c6a3f003f
2 changed files with 12 additions and 10 deletions

View file

@ -387,9 +387,9 @@ For server-side JavaScript, that limitation does not exist, and there exist othe
### Allowing the browser to render
Another benefit for in-browser scripts is that they can show a progress bar or something to the user. That's because the browser usually does all "repainting" after the script is complete.
Another benefit of splitting heavy tasks for browser scripts is that we can show a progress bar or something to the user.
So if we do a single huge function then even if it changes something, the changes are not reflected in the document till it finishes.
Usually the browser does all "repainting" after the currently running code is complete. So if we do a single huge function that changes many elements, the changes are not painted out till it finishes.
Here's the demo:
```html run
@ -402,7 +402,7 @@ Here's the demo:
for (let j = 0; j < 1e6; j++) {
i++;
// put the current i into the <div>
// (we'll talk more about innerHTML in the specific chapter, should be obvious here)
// (we'll talk about innerHTML in the specific chapter, it just writes into element here)
progress.innerHTML = i;
}
}
@ -446,9 +446,9 @@ Now the `<div>` shows increasing values of `i`.
- Methods `setInterval(func, delay, ...args)` and `setTimeout(func, delay, ...args)` allow to run the `func` regularly/once after `delay` milliseconds.
- To cancel the execution, we should call `clearInterval/clearTimeout` with the value returned by `setInterval/setTimeout`.
- Nested `setTimeout` calls is a more flexible alternative to `setInterval`. Also they can guarantee the minimal time *between* the executions.
- Zero-timeout scheduling `setTimeout(...,0)` is used to schedule the call "as soon as possible, but after the current code is complete".
- Zero-timeout scheduling `setTimeout(func, 0)` (the same as `setTimeout(func)`) is used to schedule the call "as soon as possible, but after the current code is complete".
Some use cases of `setTimeout(...,0)`:
Some use cases of `setTimeout(func)`:
- To split CPU-hungry tasks into pieces, so that the script doesn't "hang"
- To let the browser do something else while the process is going on (paint the progress bar).