H5刷新一次页面会多次触发resize怎么办?用了节流也没用?什么原因怎么改?
function throttle(func, wait) {
let previous = 0;
return function () {
let now = Date.now();
let context = this;
let args = arguments;
if (now - previous > wait) {
func.apply(context, args);
previous = now;
}
}
}
function task() {
alert(1);
location.reload();
}
window.addEventListener('resize', throttle(task, 500), false)
你写的是节流函数不是防抖, 用这个试试