-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththrottle.js
More file actions
34 lines (33 loc) · 846 Bytes
/
throttle.js
File metadata and controls
34 lines (33 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))})
([], function(){
"use strict";
return function throttle(f, threshold){
var handle, last;
return function(){
var now = new Date().getTime();
if(last && now < last + threshold){
if(handle){
clearTimeout(handle);
}
var self = this, args = arguments;
handle = setTimeout(function(){
handle = null;
last = new Date().getTime();
f.apply(self, args);
}, last + threshold - now);
}else{
last = now;
f.apply(this, arguments);
}
return {
isWaiting: function(){ return !!handle; },
destroy: function(){
if(handle){
clearTimeout(handle);
handle = null;
}
}
};
};
};
});