"效果" :单位时间内多次触发,只执行最后一次
// 参数1传入一个回调函数,参数2传入防抖生效时间
function debounce(callback, delay) {
let timer = null;
return function (...args) {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
callback.apply(this, args);
}, delay)
}
}
单位时间内只响应一次触发
// 参数1传入一个回调函数,参数2传入节流单位时间
function throttle(callback, delay) {
let timer = null;
return function (...args) {
if (timer) return;
timer = setTimeout(() => {
callback.apply(this, args);
timer = null;
}, delay)
}
}
作用:完整复制一个对象,与源对象不会发生内存共享
// 传入需要克隆的对象,返回一个新对象
function deepClone(target)