|
1 | | -# Scheduling: setTimeout and setInterval |
| 1 | +# Planificare: setTimeout și setInterval |
2 | 2 |
|
3 | | -We may decide to execute a function not right now, but at a certain time later. That's called "scheduling a call". |
| 3 | +Am putea decide să executăm o funcție nu chiar acum, ci la un anumit moment mai târziu. Acest lucru se numește "planificarea unui apel". |
4 | 4 |
|
5 | | -There are two methods for it: |
| 5 | +Există două metode pentru aceasta: |
6 | 6 |
|
7 | | -- `setTimeout` allows us to run a function once after the interval of time. |
8 | | -- `setInterval` allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval. |
| 7 | +- `setTimeout` ne permite să rulăm o funcție o dată după un interval de timp. |
| 8 | +- `setInterval` ne permite să rulăm o funcție în mod repetat, începând după intervalul de timp, apoi repetându-se încontinuu la acel interval. |
9 | 9 |
|
10 | | -These methods are not a part of JavaScript specification. But most environments have the internal scheduler and provide these methods. In particular, they are supported in all browsers and Node.js. |
| 10 | +Aceste metode nu fac parte din specificația JavaScript. Dar majoritatea mediilor au planificator intern și oferă aceste metode. În particular, ele sunt acceptate în toate browserele și în Node.js. |
11 | 11 |
|
12 | 12 | ## setTimeout |
13 | 13 |
|
14 | | -The syntax: |
| 14 | +Sintaxa: |
15 | 15 |
|
16 | 16 | ```js |
17 | 17 | let timerId = setTimeout(func|code, [delay], [arg1], [arg2], ...) |
18 | 18 | ``` |
19 | 19 |
|
20 | | -Parameters: |
| 20 | +Parametri: |
21 | 21 |
|
22 | 22 | `func|code` |
23 | | -: Function or a string of code to execute. |
24 | | -Usually, that's a function. For historical reasons, a string of code can be passed, but that's not recommended. |
| 23 | +: Funcție sau un șir de cod să fie executat. |
| 24 | +De obicei, este o funcție. Din motive istorice, poate fi trecut un șir de cod, dar nu este recomandat. |
25 | 25 |
|
26 | 26 | `delay` |
27 | | -: The delay before run, in milliseconds (1000 ms = 1 second), by default 0. |
| 27 | +: Întârzierea înainte de rulare, în milisecunde (1000 ms = 1 secundă), implicit 0. |
28 | 28 |
|
29 | 29 | `arg1`, `arg2`... |
30 | | -: Arguments for the function |
| 30 | +: Argumente pentru funcție |
31 | 31 |
|
32 | | -For instance, this code calls `sayHi()` after one second: |
| 32 | +De exemplu, acest cod apelează `sayHi()` după o secundă: |
33 | 33 |
|
34 | 34 | ```js run |
35 | 35 | function sayHi() { |
36 | | - alert('Hello'); |
| 36 | + alert('Bună ziua'); |
37 | 37 | } |
38 | 38 |
|
39 | 39 | *!* |
40 | 40 | setTimeout(sayHi, 1000); |
41 | 41 | */!* |
42 | 42 | ``` |
43 | 43 |
|
44 | | -With arguments: |
| 44 | +Cu argumente: |
45 | 45 |
|
46 | 46 | ```js run |
47 | 47 | function sayHi(phrase, who) { |
48 | 48 | alert( phrase + ', ' + who ); |
49 | 49 | } |
50 | 50 |
|
51 | 51 | *!* |
52 | | -setTimeout(sayHi, 1000, "Hello", "John"); // Hello, John |
| 52 | +setTimeout(sayHi, 1000, "Bună ziua", "John"); // Bună ziua, John |
53 | 53 | */!* |
54 | 54 | ``` |
55 | 55 |
|
56 | | -If the first argument is a string, then JavaScript creates a function from it. |
| 56 | +Dacă primul argument este un șir de caractere, atunci JavaScript creează o funcție din acesta. |
57 | 57 |
|
58 | | -So, this will also work: |
| 58 | +Așadar, și acest lucru va funcționa: |
59 | 59 |
|
60 | 60 | ```js run no-beautify |
61 | | -setTimeout("alert('Hello')", 1000); |
| 61 | +setTimeout("alert('Bună ziua')", 1000); |
62 | 62 | ``` |
63 | 63 |
|
64 | | -But using strings is not recommended, use arrow functions instead of them, like this: |
| 64 | +Dar folosirea șirurilor de caractere nu este recomandată, folosiți funcții săgeată în locul lor, astfel: |
65 | 65 |
|
66 | 66 | ```js run no-beautify |
67 | | -setTimeout(() => alert('Hello'), 1000); |
| 67 | +setTimeout(() => alert('Bună ziua'), 1000); |
68 | 68 | ``` |
69 | 69 |
|
70 | | -````smart header="Pass a function, but don't run it" |
71 | | -Novice developers sometimes make a mistake by adding brackets `()` after the function: |
| 70 | +````smart header="Treceți o funcție, dar nu o rulați" |
| 71 | +Dezvoltatorii începători fac uneori o greșeală adăugând paranteze `()` după funcție: |
72 | 72 |
|
73 | 73 | ```js |
74 | | -// wrong! |
| 74 | +// greșit! |
75 | 75 | setTimeout(sayHi(), 1000); |
76 | 76 | ``` |
77 | | -That doesn't work, because `setTimeout` expects a reference to a function. And here `sayHi()` runs the function, and the *result of its execution* is passed to `setTimeout`. In our case the result of `sayHi()` is `undefined` (the function returns nothing), so nothing is scheduled. |
| 77 | +Asta nu va funcționa, deoarece `setTimeout` se așteaptă la o referință spre o funcție. Iar aici `sayHi()` rulează funcția, iar *rezultatul execuției sale* este trecut la `setTimeout`. În cazul nostru, rezultatul lui `sayHi()` este `undefined` (funcția nu returnează nimic), deci nu se planifică nimic. |
78 | 78 | ```` |
79 | 79 |
|
80 | 80 | ### Canceling with clearTimeout |
|
0 commit comments