Skip to content

Commit 70acab8

Browse files
committed
setTimeout-setInterval
1 parent b91261d commit 70acab8

File tree

9 files changed

+158
-431
lines changed

9 files changed

+158
-431
lines changed

1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/solution.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
Using `setInterval`:
2+
Usando `setInterval`:
33

44
```js run
55
function printNumbers(from, to) {
@@ -14,11 +14,11 @@ function printNumbers(from, to) {
1414
}, 1000);
1515
}
1616

17-
// usage:
17+
// uso:
1818
printNumbers(5, 10);
1919
```
2020

21-
Using recursive `setTimeout`:
21+
Usando `setTimeout` anidado:
2222

2323

2424
```js run
@@ -34,9 +34,31 @@ function printNumbers(from, to) {
3434
}, 1000);
3535
}
3636

37-
// usage:
37+
// uso:
3838
printNumbers(5, 10);
3939
```
4040

41-
Note that in both solutions, there is an initial delay before the first output. Sometimes we need to add a line to make the first output immediately, that's easy to do.
41+
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.
4242

43+
Si también queremos que la función se ejecute inmediatamente, entonces podemos agregar una llamada adicional en una línea separada, como esta:
44+
45+
```js run
46+
function printNumbers(from, to) {
47+
let current = from;
48+
49+
function go() {
50+
alert(current);
51+
if (current == to) {
52+
clearInterval(timerId);
53+
}
54+
current++;
55+
}
56+
57+
*!*
58+
go();
59+
*/!*
60+
let timerId = setInterval(go, 1000);
61+
}
62+
63+
printNumbers(5, 10);
64+
```

1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/task.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ importance: 5
22

33
---
44

5-
# Output every second
5+
# Salida cada segundo
66

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

9-
Make two variants of the solution.
10-
11-
1. Using `setInterval`.
12-
2. Using recursive `setTimeout`.
9+
Haz dos variantes de la solución.
1310

11+
1. Usando `setInterval`.
12+
2. Usando `setTimeout` anidado.

1-js/06-advanced-functions/08-settimeout-setinterval/3-rewrite-settimeout/solution.md

Lines changed: 0 additions & 22 deletions
This file was deleted.

1-js/06-advanced-functions/08-settimeout-setinterval/3-rewrite-settimeout/task.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/solution.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

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

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

66
```js run
77
let i = 0;
88

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

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

1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/task.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ importance: 5
22

33
---
44

5-
# What will setTimeout show?
5+
# ¿Qué mostrará setTimeout?
66

7-
In the code below there's a `setTimeout` call scheduled, then a heavy calculation is run, that takes more than 100ms to finish.
7+
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.
88

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

11-
1. After the loop.
12-
2. Before the loop.
13-
3. In the beginning of the loop.
11+
1. Después del bucle.
12+
2. Antes del bucle.
13+
3. Al comienzo del bucle.
1414

1515

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

1818
```js
1919
let i = 0;
2020

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

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

0 commit comments

Comments
 (0)