Files: 306cee2cea33ec6d7ac5025d5033784134e56141 / 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