@axe/lazy-load-img
Version:
A library of lazy load image when it is necessary.
33 lines (29 loc) • 733 B
JavaScript
import "core-js/modules/es6.date.now";
export function throttle(fn, wait, trailing) {
if (wait === void 0) {
wait = 100;
}
var timer = null;
var startTime = 0;
var currTime, remainTime;
return function () {
var _this = this,
_arguments = arguments;
currTime = Date.now();
remainTime = wait - (currTime - startTime);
if (remainTime <= 0) {
if (timer) {
clearTimeout(timer);
timer = null;
}
startTime = currTime;
fn.apply(this, arguments);
} else if (!timer && trailing !== false) {
timer = setTimeout(function () {
timer = null;
startTime = Date.now();
fn.apply(_this, _arguments);
}, remainTime);
}
};
}