Files: 099e49713653c62ab39055b073d7b9d37da7d803 / animation-frame.js
502 bytesRaw
1 | var queue = [] |
2 | var running = false |
3 | var max = 1 / 60 / 2 |
4 | |
5 | module.exports = function (fn) { |
6 | if (typeof fn !== 'function') { |
7 | throw new Error('Must be a function') |
8 | } |
9 | queue.push(fn) |
10 | if (!running) { |
11 | running = true |
12 | window.requestAnimationFrame(flush) |
13 | } |
14 | } |
15 | |
16 | function flush () { |
17 | var startedAt = Date.now() |
18 | |
19 | while (queue.length && Date.now() - startedAt < max) { |
20 | queue.shift()() |
21 | } |
22 | |
23 | if (queue.length) { |
24 | window.requestAnimationFrame(flush) |
25 | } else { |
26 | running = false |
27 | } |
28 | } |
29 |
Built with git-ssb-web