-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
32 lines (27 loc) · 816 Bytes
/
index.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
import { blockFor } from "./workshop/utils/blockFor.js";
const score = document.querySelector('score-keeper');
const button = score.button;
async function afterNextPaint() {
return new Promise(resolve => {
requestAnimationFrame(() => {
setTimeout(resolve, 0);
});
// Fallback-- in case you are e.g. in a background tab
setTimeout(resolve, 1000);
});
}
async function blockInChunks(ms, chunks, signal) {
for (let i = 0; i < chunks; i++) {
if (signal.aborted) return;
blockFor(ms / chunks);
await scheduler.yield();
}
}
let abortController;
button.addEventListener("click", async () => {
abortController?.abort();
abortController = new AbortController();
score.incrementAndUpdateUI();
await afterNextPaint();
blockInChunks(1000, 10, abortController.signal);
});