Repository navigation
Scheduling: setTimeout and setInterval #483
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,14 +1,14 @@ | ||||||
|
|
||||||
| Any `setTimeout` will run only after the current code has finished. | ||||||
| Qualquer `setTimeout` só vai rodar depois que o código atual tiver terminado. | ||||||
|
|
||||||
| The `i` will be the last one: `100000000`. | ||||||
| O `i` vai ser o último valor: `100000000`. | ||||||
|
|
||||||
| ```js run | ||||||
| let i = 0; | ||||||
|
|
||||||
| setTimeout(() => alert(i), 100); // 100000000 | ||||||
|
|
||||||
| // assume that the time to execute this function is >100ms | ||||||
| // assuma que o tempo para executar essa função é >100ms | ||||||
|
Contributor
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. Same as task.md:23. This is a loop, not a function.
Suggested change
|
||||||
| for(let j = 0; j < 100000000; j++) { | ||||||
| i++; | ||||||
| } | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,25 +2,25 @@ importance: 5 | |||||
|
|
||||||
| --- | ||||||
|
|
||||||
| # What will setTimeout show? | ||||||
| # O que o setTimeout vai mostrar? | ||||||
|
|
||||||
| In the code below there's a `setTimeout` call scheduled, then a heavy calculation is run, that takes more than 100ms to finish. | ||||||
| No código abaixo, há uma chamada `setTimeout` agendada, depois um cálculo pesado é executado, que leva mais de 100ms para terminar. | ||||||
|
|
||||||
| When will the scheduled function run? | ||||||
| Quando a função agendada vai rodar? | ||||||
|
|
||||||
| 1. After the loop. | ||||||
| 2. Before the loop. | ||||||
| 3. In the beginning of the loop. | ||||||
| 1. Depois do laço. | ||||||
| 2. Antes do laço. | ||||||
| 3. No início do laço. | ||||||
|
|
||||||
|
|
||||||
| What is `alert` going to show? | ||||||
| O que o `alert` vai mostrar? | ||||||
|
|
||||||
| ```js | ||||||
| let i = 0; | ||||||
|
|
||||||
| setTimeout(() => alert(i), 100); // ? | ||||||
|
|
||||||
| // assume that the time to execute this function is >100ms | ||||||
| // assuma que o tempo para executar essa função é >100ms | ||||||
|
Contributor
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. The comment says "essa função", but what follows is a
Suggested change
|
||||||
| for(let j = 0; j < 100000000; j++) { | ||||||
| i++; | ||||||
| } | ||||||
|
|
||||||
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.
Not introduced by this PR, but this file is touched: in the third solution below,
go()(line 58) runs beforelet timerId = setInterval(go, 1000). Whenfrom == to(e.g.printNumbers(5, 5)),go()reachesclearInterval(timerId)whiletimerIdis still in the TDZ, so it throwsReferenceError: Cannot access timerId before initialization. Consider declaringlet timerId;before callinggo(), or report it upstream.