Skip to content

Commit 4c6132a

Browse files
Update article.md
1 parent 0a09cd5 commit 4c6132a

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

2-ui/99-ui-misc/03-event-loop/article.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ let i = 0;
6767
let start = Date.now();
6868

6969
function count() {
70-
// fa un lavoro pesante!
70+
//fa un lavoro pesante!
7171
for (let j = 0; j < 1e9; j++) {
7272
i++;
7373
}
@@ -80,23 +80,24 @@ count();
8080
Il browser potrebbe anche mostrare l'avviso "lo script sta impiegando troppo tempo" the script takes too long".
8181

8282

83-
Dividiamo l'operazione usando un `setTimeout` annidato:
83+
84+
Ora, dividiamo l'operazione usando un `setTimeout` annidato:
8485

8586
```js run
8687
let i = 0;
8788

8889
let start = Date.now();
8990

9091
function count() {
91-
// fai una parte del lavoro pesante (*)
92+
//fai una parte del lavoro pesante (*)
9293
do {
9394
i++;
9495
} while (i % 1e6 != 0);
9596

9697
if (i == 1e9) {
9798
alert(`Completato in ${(Date.now() - start)} ms`);
9899
} else {
99-
setTimeout(count); //schedula la nuova chiamata a count (**)
100+
setTimeout(count);//schedula la nuova chiamata a count (**)
100101
}
101102
}
102103
count();
@@ -114,52 +115,50 @@ Ora, se arriva un nuovo task da eseguire mentre il motore &egrave; occupato ad e
114115

115116
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.
116117

117-
To make them closer, let's make an improvement.
118+
Per renderli un po' pi&ugrave; facciamo un miglioramento.
119+
120+
Posizioneremo la schedualzione all'inizio del `count()`:
118121

119-
We'll move the scheduling in the beginning of the `count()`:
120122

121123
```js run
122124
let i = 0;
123125
let start = Date.now();
124126
function count() {
125-
// move the scheduling at the beginning
127+
//posizioniamo la schedulazione all'inizio
126128
if (i < 1e9 - 1e6) {
127-
setTimeout(count); // schedule the new call
129+
setTimeout(count);//scheduliamo la chiamata successiva
128130
}
129131

130132
do {
131133
i++;
132134
} while (i % 1e6 != 0);
133135

134136
if (i == 1e9) {
135-
alert("Done in " + (Date.now() - start) + 'ms');
137+
alert(`Completato in ${(Date.now() - start)} ms`);
136138
}
137-
138139
}
139-
140140
count();
141141
```
142142

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.
146144

147-
Why?
145+
Se lo esegui, &egrave; facile notare che impiega meno tempo in modo significativo.
148146

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.
150147

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&egrave;?
152149

153-
## Use case 2: progress indication
150+
Semplice: come ben sai, c'&egrave; un ritardo minimo di 4ms all'interno del browser per molte chiamate annidate di `setTimeout`. Anche se noi lo settiamo `0`, sar&agrave; di `4ms` (o qualcosa in pi&ugrave). Quindi, prima lo scheduliamo, più veloce sar&agrave; l'esecuzione.
151+
Alla fine, abbiamo diviso un task affamato di CPU in porzioni - e adesso non bloccher&agrave; l'interfaccia utente. E il suo tempo di esecuzione complessivo non &egrave; tanto pi&ugrave; lungo.
154152

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
156154

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.
158156

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.
160158

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 &egrave; grandioso, perch&egrave; la nostra fuznione pu&ograve; 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?
162160

161+
Qui c'&egrave; la demo, i cambiamenti a `i` non verrano mostrati fino a quando la funzione nno termina, cos&igrave; vedremo solamente l'ultimo valore:
163162

164163
```html run
165164
<div id="progress"></div>
@@ -177,11 +176,11 @@ Here's the demo, the changes to `i` won't show up until the function finishes, s
177176
</script>
178177
```
179178

180-
...But we also may want to show something during the task, e.g. a progress bar.
179+
...Per&ograve; potremmo volere mostrare qualcosa durante il task, ad esempio una barra di progresso.
181180

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.
183182

184-
This looks prettier:
183+
Questo sembra più carino:
185184

186185
```html run
187186
<div id="progress"></div>
@@ -191,7 +190,7 @@ This looks prettier:
191190
192191
function count() {
193192
194-
// do a piece of the heavy job (*)
193+
// fai un pezzo di lavoro pesante (*)
195194
do {
196195
i++;
197196
progress.innerHTML = i;
@@ -207,7 +206,8 @@ This looks prettier:
207206
</script>
208207
```
209208

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.
210+
211211

212212

213213
## Use case 3: doing something after the event

0 commit comments

Comments
 (0)