This commit is contained in:
Ilya Kantor 2016-11-14 16:31:21 +03:00
parent 3defacc09d
commit f99574f53b
178 changed files with 530 additions and 271 deletions

View file

@ -0,0 +1,23 @@
```js run
let i = 0;
let start = Date.now();
let timer = setInterval(count, 0);
function count() {
for(let j = 0; j < 1000000; j++) {
i++;
}
if (i == 1000000000) {
alert("Done in " + (Date.now() - start) + 'ms');
cancelInterval(timer);
}
}
```

View file

@ -0,0 +1,32 @@
importance: 4
---
# Rewrite setTimeout with setInterval
Here's the function that uses nested `setTimeout` to split a job into pieces.
Rewrite it to `setInterval`:
```js run
let i = 0;
let start = Date.now();
function count() {
if (i == 1000000000) {
alert("Done in " + (Date.now() - start) + 'ms');
} else {
setTimeout(count, 0);
}
// a piece of heavy job
for(let j = 0; j < 1000000; j++) {
i++;
}
}
count();
```