-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Scheduling: setTimeout and setInterval #3423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bogdanbacosca
wants to merge
7
commits into
javascript-tutorial:master
Choose a base branch
from
bogdanbacosca:timeout
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d801b5d
line 101
bogdanbacosca 13264dc
line 130
bogdanbacosca ec51052
line 211
bogdanbacosca 0dd79b2
line 259
bogdanbacosca 6f3cda0
line 261
bogdanbacosca feab961
line 280
bogdanbacosca 33d655f
lines 282 and 284
bogdanbacosca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,7 +98,7 @@ clearTimeout(timerId); | |
alert(timerId); // same identifier (doesn't become null after canceling) | ||
``` | ||
|
||
As we can see from `alert` output, in a browser the timer identifier is a number. In other environments, this can be something else. For instance, Node.js returns a timer object with additional methods. | ||
As we can see from the `alert` output in our browser, the timer identifier is a number. In other environments, this can be something else. For instance, Node.js returns a timer object with additional methods. | ||
|
||
Again, there is no universal specification for these methods, so that's fine. | ||
|
||
|
@@ -127,7 +127,7 @@ setTimeout(() => { clearInterval(timerId); alert('stop'); }, 5000); | |
``` | ||
|
||
```smart header="Time goes on while `alert` is shown" | ||
In most browsers, including Chrome and Firefox the internal timer continues "ticking" while showing `alert/confirm/prompt`. | ||
In most browsers including Chrome and Firefox, the internal timer continues "ticking" while showing `alert/confirm/prompt`. | ||
|
||
So if you run the code above and don't dismiss the `alert` window for some time, then the next `alert` will be shown immediately as you do it. The actual interval between alerts will be shorter than 2 seconds. | ||
``` | ||
|
@@ -208,7 +208,7 @@ Did you notice? | |
|
||
That's normal, because the time taken by `func`'s execution "consumes" a part of the interval. | ||
|
||
It is possible that `func`'s execution turns out to be longer than we expected and takes more than 100ms. | ||
It is possible that `func`'s execution to be longer than we expected and take more than 100ms. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. disagree, leave as original |
||
|
||
In this case the engine waits for `func` to complete, then checks the scheduler and if the time is up, runs it again *immediately*. | ||
|
||
|
@@ -256,9 +256,9 @@ The first line "puts the call into calendar after 0ms". But the scheduler will o | |
There are also advanced browser-related use cases of zero-delay timeout, that we'll discuss in the chapter <info:event-loop>. | ||
|
||
````smart header="Zero delay is in fact not zero (in a browser)" | ||
In the browser, there's a limitation of how often nested timers can run. The [HTML Living Standard](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers) says: "after five nested timers, the interval is forced to be at least 4 milliseconds.". | ||
In the browser, there's a limitation for how often nested timers can run. The [HTML Living Standard](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers) says: "after five nested timers, the interval is forced to be at least 4 milliseconds.". | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. disagree |
||
|
||
Let's demonstrate what it means with the example below. The `setTimeout` call in it re-schedules itself with zero delay. Each call remembers the real time from the previous one in the `times` array. What do the real delays look like? Let's see: | ||
Let's demonstrate what it means with the example below. The `setTimeout` call in it re-schedules itself with zero delay. Each iteration remembers the real time interval between calls in the `times` array. What do the real delays look like? Let's see: | ||
|
||
```js run | ||
let start = Date.now(); | ||
|
@@ -277,11 +277,11 @@ setTimeout(function run() { | |
|
||
First timers run immediately (just as written in the spec), and then we see `9, 15, 20, 24...`. The 4+ ms obligatory delay between invocations comes into play. | ||
|
||
The similar thing happens if we use `setInterval` instead of `setTimeout`: `setInterval(f)` runs `f` few times with zero-delay, and afterwards with 4+ ms delay. | ||
The same thing happens if we use `setInterval` instead of `setTimeout`: `setInterval(f)` runs `f` few times with zero-delay, and afterwards with 4+ ms delay. | ||
|
||
That limitation comes from ancient times and many scripts rely on it, so it exists for historical reasons. | ||
This limitation comes from ancient times and many scripts rely on it, so it exists for historical reasons. | ||
|
||
For server-side JavaScript, that limitation does not exist, and there exist other ways to schedule an immediate asynchronous job, like [setImmediate](https://nodejs.org/api/timers.html#timers_setimmediate_callback_args) for Node.js. So this note is browser-specific. | ||
For server-side JavaScript, this limitation does not exist, and there are other ways to schedule an immediate asynchronous job, like [setImmediate](https://nodejs.org/api/timers.html#timers_setimmediate_callback_args) for Node.js. So this note is browser-specific. | ||
```` | ||
|
||
## Summary | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO should have both: