
// 节流
function thro(func, wait) {
let timeOut;
return function () {
if (!timeOut) {
timeOut = setTimeout(() => {
func();
timeOut = null;
}, wait);
}
};
}
function handle() {
console.log(Math.random());
}
document.getElementById("button").onclick = thro(handle, 2000);
本文介绍了一个名为'thro'的节流函数,它用于控制函数的执行频率,配合定时器实现在2秒延迟后执行handle()函数。通过这个实例,读者可以理解如何在JavaScript中管理异步操作的时机。
556

被折叠的 条评论
为什么被折叠?



