<script>
function debounce (fn, delay) {//防抖
let timer = null;
let firstTime = false;
return function (...args) {
let context = this;
if (firstTime) {
// 第一次加载
fn.apply(context, args);
return firstTime = false;
}
console.log(timer)
if (timer) {
// 定时器正在执行中,跳过
return;
}
timer = setTimeout(() => {
clearTimeout(timer);
timer = null;
fn.apply(context, args);
}, delay);
console.log(timer)
};
}
var doms = document.getElementById('demo')
doms.addEventListener('click', debounce(loctionthrottleFn, 1000))
function loctionthrottleFn () {
let _this =this
console.log(123)
}
</script>
<body>
<div id="app"></div>
<div id="demo">惦记我</div>
</body>