Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Scheduling: setTimeout and setInterval#202

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vplentinax merged 2 commits intojavascript-tutorial:masterfromvplentinax:setTime
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@

Using `setInterval`:
Usando `setInterval`:

```js run
function printNumbers(from, to) {
Expand All@@ -14,12 +14,11 @@ function printNumbers(from, to) {
}, 1000);
}

//usage:
//uso:
printNumbers(5, 10);
```

Using nested `setTimeout`:

Usando `setTimeout` anidado:

```js run
function printNumbers(from, to) {
Expand All@@ -34,13 +33,13 @@ function printNumbers(from, to) {
}, 1000);
}

//usage:
//uso:
printNumbers(5, 10);
```

Note that in both solutions, there is an initial delay before the first output. The function is called after`1000ms`the first time.
Tenga en cuenta que en ambas soluciones, hay un retraso inicial antes de la primera salida. La función se llama después de`1000ms`la primera vez.

If we also want the function to run immediately, then we can add an additional call on a separate line, like this:
Si también queremos que la función se ejecute inmediatamente, entonces podemos agregar una llamada adicional en una línea separada, como esta:

```js run
function printNumbers(from, to) {
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,11 @@ importance: 5

---

#Output every second
#Salida cada segundo

Write a function `printNumbers(from, to)`that outputs a number every second, starting from `from`and ending with `to`.
Escriba una función `printNumbers(from, to)`que genere un número cada segundo, comenzando desde `from`y terminando con `to`.

Make two variants of the solution.
Haz dos variantes de la solución.

1.Using `setInterval`.
2.Using nested`setTimeout`.
1.Usando `setInterval`.
2.Usando`setTimeout` anidado.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@

Any `setTimeout`will run only after the current code has finished.
Cualquier `setTimeout`se ejecutará solo después de que el código actual haya finalizado.

The `i`will be the last one: `100000000`.
La `i`será la última:`100000000`.

```js run
let i = 0;

setTimeout(() => alert(i), 100); // 100000000

//assume that the time to execute this function is >100ms
//supongamos que el tiempo para ejecutar esta función es> 100 ms
for(let j = 0; j < 100000000; j++) {
i++;
}
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,25 +2,25 @@ importance: 5

---

#What will setTimeout show?
#¿Qué mostrará setTimeout?

In the code below there's a`setTimeout` call scheduled, then a heavy calculation is run, that takes more than 100ms to finish.
En el siguiente código hay una llamada programada`setTimeout`, luego se ejecuta un cálculo pesado, que demora más de 100 ms en finalizar.

When will the scheduled function run?
¿Cuándo se ejecutará la función programada?

1.After the loop.
2.Before the loop.
3.In the beginning of the loop.
1.Después del bucle.
2.Antes del bucle.
3.Al comienzo del bucle.


What is `alert` going to show?
¿Qué va a mostrar "alerta"?

```js
let i = 0;

setTimeout(() => alert(i), 100); // ?

//assume that the time to execute this function is >100ms
//supongamos que el tiempo para ejecutar esta función es> 100 ms
for(let j = 0; j < 100000000; j++) {
i++;
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp