-
Notifications
You must be signed in to change notification settings - Fork 5
/
demo.html
70 lines (59 loc) · 2.04 KB
/
demo.html
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
<!DOCTYPE html>
<html>
<head>
<title>Afterframe Demo</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="app"><ol id="log"></ol></div>
<script src="dist/afterframe.umd.js" type="text/javascript"></script>
<script>
/**
* @returns {Promise<number>}
*/
function afterFrameAsync() {
return new Promise(resolve => afterFrame(time => resolve(time)));
}
</script>
<script>
// main.js
const list = document.getElementById("log");
const formatTime = num => num.toFixed(2);
function appendLog(log) {
const text = document.createTextNode(log);
const logItem = document.createElement("li");
logItem.appendChild(text);
list.appendChild(logItem);
}
async function callback1(time) {
appendLog("callback1: " + formatTime(time));
// Queue up a callback from within another callback to validate
// proper behavior
time = await afterFrameAsync();
appendLog("await 1: " + formatTime(time));
// Verify the Promise form of afterFrame actually does delay until
// the next frame
time = await afterFrameAsync();
appendLog("await 2: " + formatTime(time));
}
function callback2(time) {
appendLog("callback2: " + formatTime(time));
}
function callback3(time) {
appendLog("callback3: " + formatTime(time));
}
function main() {
// Since these callbacks are all queued in the same frame, they
// should be called with the same time parameter. The first two
// (callback 1 & 2) are queued in the same JS stack and so will be
// flushed together. `callback3` should invoked with the same time
// since it is run when the JS microTask queue is flushed at the
// end of this stack, in the same frame as callback 1 and 2.
afterFrame(callback1);
afterFrame(callback2);
Promise.resolve().then(() => afterFrame(callback3));
}
main();
</script>
</body>
</html>