Skip to content

Commit 06609cf

Browse files
authored
Merge pull request #88 from Luma1970/master
Scheduling: setTimeout and setInterval
2 parents a8a7c0e + 606ce44 commit 06609cf

File tree

5 files changed

+175
-175
lines changed

5 files changed

+175
-175
lines changed
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
11

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

44
```js run
5-
function printNumbers(from, to) {
6-
let current = from;
5+
function stampaNumeri(da, a) {
6+
let attuale = da;
77

88
let timerId = setInterval(function() {
9-
alert(current);
10-
if (current == to) {
9+
alert(attuale);
10+
if (attuale == a) {
1111
clearInterval(timerId);
1212
}
13-
current++;
13+
attuale++;
1414
}, 1000);
1515
}
1616

17-
// usage:
18-
printNumbers(5, 10);
17+
// utilizzo:
18+
stampaNumeri(5, 10);
1919
```
2020

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

2323

2424
```js run
25-
function printNumbers(from, to) {
26-
let current = from;
25+
function stampaNumeri(da, a) {
26+
let attuale = da;
2727

28-
setTimeout(function go() {
29-
alert(current);
30-
if (current < to) {
31-
setTimeout(go, 1000);
28+
setTimeout(function vai() {
29+
alert(attuale);
30+
if (attuale < a) {
31+
setTimeout(vai, 1000);
3232
}
33-
current++;
33+
attuale++;
3434
}, 1000);
3535
}
3636

37-
// usage:
38-
printNumbers(5, 10);
37+
// utilizzo:
38+
stampaNumeri(5, 10);
3939
```
4040

41-
Note that in both solutions, there is an initial delay before the first output. The function is called after `1000ms` the first time.
41+
Nota che in entrambe le soluzioni c'è un ritardo iniziale prima del primo output. La funzione viene eseguita la prima volta dopo `1000ms`.
4242

43-
If we also want the function to run immediately, then we can add an additional call on a separate line, like this:
43+
Se vogliamo che la funzione venga eseguita subito, possiamo aggiugere una chiamata addizionale su di una linea separata, come questa:
4444

4545
```js run
46-
function printNumbers(from, to) {
47-
let current = from;
46+
function stampaNumeri(da, a) {
47+
let attuale = da;
4848

49-
function go() {
50-
alert(current);
51-
if (current == to) {
49+
function vai() {
50+
alert(attule);
51+
if (attuale == a) {
5252
clearInterval(timerId);
5353
}
54-
current++;
54+
attuale++;
5555
}
5656

5757
*!*
58-
go();
58+
vai();
5959
*/!*
60-
let timerId = setInterval(go, 1000);
60+
let timerId = setInterval(vai, 1000);
6161
}
6262

63-
printNumbers(5, 10);
63+
stampaNumeri(5, 10);
6464
```

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

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

33
---
44

5-
# Output every second
5+
# Output ogni secondo
66

7-
Write a function `printNumbers(from, to)` that outputs a number every second, starting from `from` and ending with `to`.
7+
Scrivi una funzione `stampaNumeri(da, a)` che produca un numero ogni secondo, partendo da `da` e finendo con `a`.
88

9-
Make two variants of the solution.
9+
Crea due varianti della soluzione.
1010

11-
1. Using `setInterval`.
12-
2. Using recursive `setTimeout`.
11+
1. Usando `setInterval`.
12+
2. Usando `setTimeout` ricorsivo.
1313

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+
Ogni `setTimeout` verrà eseguito solo dopo che il codice corrente è completo.
33

4-
The `i` will be the last one: `100000000`.
4+
La `i` sarà l'ultimo: `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+
// ipotizza che il tempo necessario a eseguire questa funzione sia >100ms
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+
# Cosa mostrerà 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+
Nel codice qui sotto è pianificata una chiamata con `setTimeout`, poi viene eseguito un calcolo pesante, che richiede più di 100ms per essere completato.
88

9-
When will the scheduled function run?
9+
Quando verrà eseguita la funzione pianificata?
1010

11-
1. After the loop.
12-
2. Before the loop.
13-
3. In the beginning of the loop.
11+
1. Dopo il loop.
12+
2. Prima del loop.
13+
3. All'inizio del loop.
1414

1515

16-
What is `alert` going to show?
16+
Cosa mostrerà l'`alert`?
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+
// ipotizza che il tempo necessario a eseguire questa funzione sia >100ms
2424
for(let j = 0; j < 100000000; j++) {
2525
i++;
2626
}

0 commit comments

Comments
 (0)