2017-08-11 18:18:09 +00:00
|
|
|
export default function throttle(fn, threshhold = 250, scope) {
|
2017-08-18 14:50:20 +00:00
|
|
|
let last;
|
|
|
|
let deferTimer;
|
|
|
|
|
|
|
|
return (...args) => {
|
2017-08-11 18:18:09 +00:00
|
|
|
const context = scope || this;
|
|
|
|
|
2017-08-18 14:50:20 +00:00
|
|
|
const now = +new Date();
|
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
if (last && now < last + threshhold) {
|
|
|
|
// hold on to it
|
|
|
|
clearTimeout(deferTimer);
|
|
|
|
deferTimer = setTimeout(() => {
|
|
|
|
last = now;
|
|
|
|
fn.apply(context, args);
|
|
|
|
}, threshhold);
|
|
|
|
} else {
|
|
|
|
last = now;
|
|
|
|
fn.apply(context, args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|