gpt4 book ai didi

javascript - setTimeout/clearTimeout 贵吗?

转载 作者:可可西里 更新时间:2023-11-01 02:44:59 24 4
gpt4 key购买 nike

我有如下内容:

var myTimeout;
function delayStuffUntil( when ){
if( myTimeout) clearTimeout( myTimeout );
myTimeout = setTimeout( stuff, when - Date.now() );
}

delayStuffUntil 会被调用很多次,很可能会连续多次使用相同的 when 值调用它。

setTimeout/clearTimeout 是否足够昂贵以至于我应该检查当前的 when 与我最后一个 when 的值>(并且只有在不同时才更改计时器)?

我考虑过这样做,但事实是 when 比较起来有点麻烦,而且我认为过早的优化是万恶之源,所以我可能会在我做的时候工作不需要。

最佳答案

setTimeoutclearTimeout 本身并不是很昂贵。

真正需要担心的是函数“stuff”。

如果东西需要很长时间才能运行,那么如果过于频繁地调用它可能会阻塞 UI。

关于javascript - setTimeout/clearTimeout 贵吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9072472/

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