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: 2-ui/99-ui-misc/03-event-loop/article.md
+27-27Lines changed: 27 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,7 @@ let i = 0;
67
67
let start =Date.now();
68
68
69
69
functioncount() {
70
-
//fa un lavoro pesante!
70
+
//fa un lavoro pesante!
71
71
for (let j =0; j <1e9; j++) {
72
72
i++;
73
73
}
@@ -80,23 +80,24 @@ count();
80
80
Il browser potrebbe anche mostrare l'avviso "lo script sta impiegando troppo tempo" the script takes too long".
81
81
82
82
83
-
Dividiamo l'operazione usando un `setTimeout` annidato:
83
+
84
+
Ora, dividiamo l'operazione usando un `setTimeout` annidato:
84
85
85
86
```js run
86
87
let i =0;
87
88
88
89
let start =Date.now();
89
90
90
91
functioncount() {
91
-
//fai una parte del lavoro pesante (*)
92
+
//fai una parte del lavoro pesante (*)
92
93
do {
93
94
i++;
94
95
} while (i %1e6!=0);
95
96
96
97
if (i ==1e9) {
97
98
alert(`Completato in ${(Date.now() - start)} ms`);
98
99
} else {
99
-
setTimeout(count);//schedula la nuova chiamata a count (**)
100
+
setTimeout(count);//schedula la nuova chiamata a count (**)
100
101
}
101
102
}
102
103
count();
@@ -114,52 +115,50 @@ Ora, se arriva un nuovo task da eseguire mentre il motore è occupato ad e
114
115
115
116
La cosa ragguardevole è che entrambe le varianti -- con e senza la divsione del lavoro di `setTimeout` -- sono comparabili in termini di tempo. Complessivamente, non esiste molta differenza nel tempo di conteggio.
116
117
117
-
To make them closer, let's make an improvement.
118
+
Per renderli un po' più facciamo un miglioramento.
119
+
120
+
Posizioneremo la schedualzione all'inizio del `count()`:
118
121
119
-
We'll move the scheduling in the beginning of the `count()`:
120
122
121
123
```js run
122
124
let i =0;
123
125
let start =Date.now();
124
126
functioncount() {
125
-
// move the scheduling at the beginning
127
+
//posizioniamo la schedulazione all'inizio
126
128
if (i <1e9-1e6) {
127
-
setTimeout(count);// schedule the new call
129
+
setTimeout(count);//scheduliamo la chiamata successiva
128
130
}
129
131
130
132
do {
131
133
i++;
132
134
} while (i %1e6!=0);
133
135
134
136
if (i ==1e9) {
135
-
alert("Done in "+(Date.now() - start)+'ms');
137
+
alert(`Completato in ${(Date.now() - start)} ms`);
136
138
}
137
-
138
139
}
139
-
140
140
count();
141
141
```
142
142
143
-
Now when we start to `count()` and see that we'll need to `count()` more, we schedule that immediately, before doing the job.
144
-
145
-
If you run it, it's easy to notice that it takes significantly less time.
143
+
Adesso quando cominciamo con `count()` e vediamo che abbiamo bisogno di richiamarlo più `count()`, lo scheduliamo subito, prima di fare il lavoro.
146
144
147
-
Why?
145
+
Se lo esegui, è facile notare che impiega meno tempo in modo significativo.
148
146
149
-
That's simple: as you remember, there's the in-browser minimal delay of 4ms for many nested `setTimeout` calls. Even if we set `0`, it's `4ms` (or a bit more). So the earlier we schedule it - the faster it runs.
150
147
151
-
Finally, we've split a CPU-hungry task into parts - now it doesn't block the user interface. And its overall execution time isn't much longer.
148
+
Perchè?
152
149
153
-
## Use case 2: progress indication
150
+
Semplice: come ben sai, c'è un ritardo minimo di 4ms all'interno del browser per molte chiamate annidate di `setTimeout`. Anche se noi lo settiamo `0`, sarà di `4ms` (o qualcosa in più). Quindi, prima lo scheduliamo, più veloce sarà l'esecuzione.
151
+
Alla fine, abbiamo diviso un task affamato di CPU in porzioni - e adesso non bloccherà l'interfaccia utente. E il suo tempo di esecuzione complessivo non è tanto più lungo.
154
152
155
-
Another benefit of splitting heavy tasks for browser scripts is that we can show progress indication.
153
+
## Caso d'uso 2: Indicazione dei progressi di una operazione
156
154
157
-
Usually the browser renders after the currently running code is complete. Doesn't matter if the task takes a long time. Changes to DOM are painted only after the task is finished.
155
+
Un altro beneficio di dividere task pesanti per gli script del browser è che possiamo mostrare i progressi di completamento.
158
156
159
-
From one hand, that's great, because our function may create many elements, add them one-by-one to the document and change their styles -- the visitor won't see any "intermediate", unfinished state. An important thing, right?
157
+
Solitamente il browser renderizza dopo che il codice in esecuzine viene completato. Non importa se il task impiega tanto tempo. Le modifice al DOM vengono mostrate solo dopo che il task è terminato.
160
158
161
-
Here's the demo, the changes to `i` won't show up until the function finishes, so we'll see only the last value:
159
+
Da una parte, questo è grandioso, perchè la nostra fuznione può molti elementi, aggiungerli uno alla volta al documento e cambiarne gli stili -- il visitatore non vorrebbe mai vedere uno stadio "intermedio", incompleto. Una cosa importante, giusto?
162
160
161
+
Qui c'è la demo, i cambiamenti a `i` non verrano mostrati fino a quando la funzione nno termina, così vedremo solamente l'ultimo valore:
163
162
164
163
```html run
165
164
<divid="progress"></div>
@@ -177,11 +176,11 @@ Here's the demo, the changes to `i` won't show up until the function finishes, s
177
176
</script>
178
177
```
179
178
180
-
...But we also may want to show something during the task, e.g. a progress bar.
179
+
...Però potremmo volere mostrare qualcosa durante il task, ad esempio una barra di progresso.
181
180
182
-
If we split the heavy task into pieces using `setTimeout`, then changes are painted out in-between them.
181
+
Se noi dividiamo il task pesante in pezzi usando `setTimeout`, allora tra essi, verranno mostrate le variazioni.
183
182
184
-
This looks prettier:
183
+
Questo sembra più carino:
185
184
186
185
```html run
187
186
<divid="progress"></div>
@@ -191,7 +190,7 @@ This looks prettier:
191
190
192
191
functioncount() {
193
192
194
-
//do a piece of the heavy job (*)
193
+
//fai un pezzo di lavoro pesante (*)
195
194
do {
196
195
i++;
197
196
progress.innerHTML= i;
@@ -207,7 +206,8 @@ This looks prettier:
207
206
</script>
208
207
```
209
208
210
-
Now the `<div>` shows increasing values of `i`, a kind of a progress bar.
209
+
Adesso il `<div>` mostra via via, valori crescenti di `i`, una sorta di barra di caricamento.
0 commit comments