You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/08-settimeout-setinterval/article.md
+21-21
Original file line number
Diff line number
Diff line change
@@ -157,15 +157,15 @@ Metoda imbricată `setTimeout` este mai flexibilă decât `setInterval`. În ace
157
157
158
158
De exemplu, trebuie să scriem un serviciu care să trimită o solicitare către server la fiecare 5 secunde pentru a cere date, dar în cazul în care serverul este supraîncărcat, ar trebui să crească intervalul la 10, 20, 40 de secunde...
159
159
160
-
Here's the pseudocode:
160
+
Iată pseudocodul:
161
161
```js
162
162
let delay =5000;
163
163
164
164
let timerId =setTimeout(functionrequest() {
165
165
...send request...
166
166
167
167
if (request failed due to server overload) {
168
-
//increase the interval to the next run
168
+
//crește intervalul până la următoarea execuție
169
169
delay *=2;
170
170
}
171
171
@@ -175,11 +175,11 @@ let timerId = setTimeout(function request() {
175
175
```
176
176
177
177
178
-
And if the functions that we're scheduling are CPU-hungry, then we can measure the time taken by the execution and plan the next call sooner or later.
178
+
Iar dacă funcțiile pe care le planificăm sunt mari consumatoare de CPU, atunci putem măsura timpul de execuție și putem planifica următorul apel mai devreme sau mai târziu.
179
179
180
-
**Nested `setTimeout` allows to set the delay between the executions more precisely than`setInterval`.**
180
+
**`setTimeout`-ul imbricat permite stabilirea întârzierii dintre execuții mai precis decât`setInterval`.**
181
181
182
-
Let's compare two code fragments. The first one uses`setInterval`:
182
+
Să comparăm două fragmente de cod. Primul utilizează`setInterval`:
183
183
184
184
```js
185
185
let i =1;
@@ -188,7 +188,7 @@ setInterval(function() {
188
188
}, 100);
189
189
```
190
190
191
-
The second one uses nested `setTimeout`:
191
+
Al doilea utilizează `setTimeout` imbricat:
192
192
193
193
```js
194
194
let i =1;
@@ -198,41 +198,41 @@ setTimeout(function run() {
198
198
}, 100);
199
199
```
200
200
201
-
For`setInterval`the internal scheduler will run `func(i++)`every 100ms:
201
+
Pentru`setInterval`planificatorul intern va rula `func(i++)`la fiecare 100 ms:
202
202
203
203

204
204
205
-
Did you notice?
205
+
Ați observat?
206
206
207
-
**The real delay between`func`calls for `setInterval`is less than in the code!**
207
+
**Întârzierea reală între apelurile`func`pentru `setInterval`este mai mică decât în cod!**
208
208
209
-
That's normal, because the time taken by `func`'s execution "consumes" a part of the interval.
209
+
Este normal, pentru că timpul ocupat de execuția lui `func` "consumă" o parte din interval.
210
210
211
-
It is possible that `func`'s execution turns out to be longer than we expected and takes more than 100ms.
211
+
Este posibil ca execuția lui `func` să se dovedească a fi mai lungă decât ne așteptam și să dureze mai mult de 100ms.
212
212
213
-
In this case the engine waits for `func`to complete, then checks the scheduler and if the time is up, runs it again *immediately*.
213
+
În acest caz motorul așteaptă ca `func`să se termine, apoi verifică planificatorul și dacă timpul a expirat, îl rulează din nou *imediat*.
214
214
215
-
In the edge case, if the function always executes longer than `delay` ms, then the calls will happen without a pause at all.
215
+
În cazul marginal, dacă funcția se execută întotdeauna mai îndelungat decât `delay` ms, atunci apelurile se vor întâmpla fără nicio pauză.
216
216
217
-
And here is the picture for the nested `setTimeout`:
217
+
Și iată imaginea pentru `setTimeout` imbricat:
218
218
219
219

220
220
221
-
**The nested `setTimeout` guarantees the fixed delay (here 100ms).**
That's because a new call is planned at the end of the previous one.
223
+
Asta pentru că un nou apel este planificat la sfârșitul celui precedent.
224
224
225
-
````smart header="Garbage collection and setInterval/setTimeout callback"
226
-
When a function is passed in `setInterval/setTimeout`, an internal reference is created to it and saved in the scheduler. It prevents the function from being garbage collected, even if there are no other references to it.
225
+
````smart header="Colectarea gunoiului și callback-ul setInterval/setTimeout"
226
+
Atunci când o funcție este predată în `setInterval/setTimeout`, este creată o referință internă la aceasta și salvată în planificator. Aceasta previne ca funcția să fie colectată la gunoi, chiar dacă nu există alte referințe la ea.
227
227
228
228
```js
229
-
// the function stays in memory until the scheduler calls it
229
+
// funcția rămâne în memorie până când planificatorul o apelează
230
230
setTimeout(function() {...}, 100);
231
231
```
232
232
233
-
For `setInterval` the function stays in memory until `clearInterval` is called.
233
+
Pentru `setInterval`, funcția rămâne în memorie până când este apelată `clearInterval`.
234
234
235
-
There's a side effect. A function references the outer lexical environment, so, while it lives, outer variables live too. They may take much more memory than the function itself. So when we don't need the scheduled function anymore, it's better to cancel it, even if it's very small.
235
+
Există un efect secundar. O funcție face referire la mediul lexical extern, astfel încât, în timp ce ea trăiește, trăiesc și variabilele externe. Acestea pot ocupa mult mai multă memorie decât funcția însăși. Deci atunci când nu mai avem nevoie de funcția programată, este mai bine să o anulăm, chiar dacă este foarte mică.
0 commit comments