防抖
function throttle(func, timeout) {
let timeId = null;
return function() {
if (!timeId) {
timeId = setTimeout(() => {
func.apply(this, arguments);
timeId = null;
}, timeout);
}
};
}
function throttle(func, delay) {
let pre = Date.now();
return function() {
let now = Date.now();
if (now - pre >= delay) {
func.apply(this, arguments);
pre = now;
}
};
}
节流
function dobounce(func, timeout) {
let timeId = null;
return function() {
if (timeId) {
clearTimeout(timeId);
}
timeId = setTimeout(() => {
func.apply(this, arguments);
}, timeout);
};
}
function dobounce(func, delay) {
let pre = Date.now();
return function() {
let now = Date.now();
if (now - pre >= delay) {
func.apply(this, arguments);
}
pre = now;
};
}