diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-08-23 12:43:53 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-23 12:43:53 -0400 |
commit | 934355dd09aa8b7e8993759b678af063b56b9fc6 (patch) | |
tree | cf08d4559a455686dd86c52a543ab6868b6c5df9 /ext | |
parent | 1ab853a4d5bb0fdfe329282be2ae213ec1563252 (diff) |
Add promiseAnimationFrame function (#752)
Diffstat (limited to 'ext')
-rw-r--r-- | ext/mixed/js/core.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index 5bee4670..c5c6fef2 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -258,6 +258,34 @@ function promiseTimeout(delay, resolveValue) { return promise; } +function promiseAnimationFrame(timeout=null) { + return new Promise((resolve) => { + let timer = null; + let frameRequest = null; + const onFrame = (time) => { + frameRequest = null; + if (timer !== null) { + clearTimeout(timer); + timer = null; + } + resolve({time, timeout: false}); + }; + const onTimeout = () => { + timer = null; + if (frameRequest !== null) { + cancelAnimationFrame(frameRequest); + frameRequest = null; + } + resolve({time: timeout, timeout: true}); + }; + + frameRequest = requestAnimationFrame(onFrame); + if (typeof timeout === 'number') { + timer = setTimeout(onTimeout, timeout); + } + }); +} + /* * Common classes |