gpt4 book ai didi

javascript - 如何只触发最后一个 setTimeout

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:08:33 26 4
gpt4 key购买 nike

window.addEventListener('resize', onResize, false);
function onResize() {
timer = setTimeout(function() {
console.log('fire timer');
}, 1000);
}

调整大小事件的触发速度非常快。所以每 1000 毫秒就会有很多定时器触发。如何只触发最后一个计时器?

最佳答案

清除每次调用的计时器

var timer;

function onResize() {

clearTimeout(timer);

//...

timer = setTimeout(function() {
console.log('fire timer');
}, 1000);
}

(timer 可以为 null 或未定义)。

关于javascript - 如何只触发最后一个 setTimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17129204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com