函数防抖与函数节流
debounce
function debounce(func, delay = 500) {
var timeout;
return function (e) {
clearTimeout(timeout);
var context = this,
args = arguments;
timeout = setTimeout(function () {
func.apply(context, args);
}, delay)
};
};
var func = debounce(function (v) {
console.log('--', v)
});
var i = 0;
function change() {
func(i);
setTimeout(() => {
i < 10 && change();
}, i*100);
i++;
}
change();
// 5,6,7,8,9throttle
Last updated