-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequestIddleCalback.js
80 lines (63 loc) · 2.01 KB
/
requestIddleCalback.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
function getColor(color) {
return [
`color: ${color};`,
"border-bottom: 1px solid #000;",
"padding: 3px 10px;"
].join("");
}
function logger(str, color = "#000") {
console.log("%c%s", getColor(color), str);
}
var task = [
() => console.log("task with iddle 1"),
() => console.log("task with iddle 2"),
() => console.log("task with iddle 3")
];
function backgroundTask(deadline, tasks, name) {
Promise.resolve().then(() => logger("micro task in backgroundTask", "blue")); // промис внутри requestIdleCallback - выполняется немедленно, даже, если времени не осталось
setTimeout(() => logger(`macro task in backgroundTask ${name}`, "green"), 0); // макро задача выполняется в самом конце, как обычно
while (deadline.timeRemaining() > 0 && tasks.length > 0) {
tasks.shift()();
}
if (tasks.length > 0) {
requestIdleCallback(backgroundTask);
}
}
requestIdleCallback(deadline => {
logger("requestIdleCallback begin", "#673ab7");
backgroundTask(deadline, [...task], "first");
});
requestIdleCallback(deadline => {
logger("requestIdleCallback2 begin", "#673ab7");
backgroundTask(deadline, [...task], "second");
});
const block = document.querySelector(".block");
const num = Math.floor(Math.random() * 5);
const arr = Array.from({ length: 20000 });
let count = 0;
function asyncForEach(arr, cb) {
arr.forEach((i, index) => {
setTimeout(() => cb(index), 0);
});
}
function syncForEach(arr, cb) {
arr.forEach((i, index) => {
cb(index);
});
}
logger("sync start");
Promise.resolve().then(() => logger("micro task 1", "blue"));
setTimeout(() => logger("macro task 1", "green"), 0);
const cb = index => {
if (index === arr.length - 1) {
logger(`asyncForEach end (macro tasks) - ${count}`, "red");
if (count < 2) {
count++;
asyncForEach(arr, cb);
}
}
const result = index ** num;
block.innerHTML = result;
};
asyncForEach(arr, cb);
logger("sync end");