Files: 8496537aecbe69ca78b08cb864ed19471e64bd10 / once-idle.js
497 bytesRaw
1 | var queue = [] |
2 | var running = false |
3 | var max = 1000 / 60 |
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.requestIdleCallback(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.requestIdleCallback(flush) |
25 | } else { |
26 | running = false |
27 | } |
28 | } |
29 |
Built with git-ssb-web