Files: 86a0d059a6618c5b0e01793ac5a710c66c9b4dda / bounce.js
456 bytesRaw
1 | //call fn only every interval, call immediately if it hasn't |
2 | //been called within interval, and if it's called again within |
3 | // |
4 | module.exports = function (fn, interval) { |
5 | var _ts = 0, timer |
6 | function go () { |
7 | clearTimeout(timer); |
8 | timeout = null; |
9 | _ts = Date.now(); |
10 | fn() |
11 | } |
12 | |
13 | return function () { |
14 | var diff = Date.now() - _ts |
15 | if(diff > interval) go() |
16 | else if(!timer) { |
17 | timer = setTimeout(go, interval - diff) |
18 | } |
19 | } |
20 | } |
21 | |
22 |
Built with git-ssb-web