git ssb

6+

Dominic / epidemic-broadcast-trees



Tree: ec16e1429ac292be5b2db5a83aa29a9f0ffffbf6

Files: ec16e1429ac292be5b2db5a83aa29a9f0ffffbf6 / 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//
4module.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