gpt4 book ai didi

Javascript 计时器算法帮助,做一次,然后 clearInterval

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:26:21 25 4
gpt4 key购买 nike

在 mousemove 或 scroll 上我想重置一个定时器,如果它没有运行并且在定时器函数内运行一次函数。到目前为止我有这个...

var timerId,
lastActive = new Date().getTime(),
token;

var timerFunc = function () {

var currentTime = new Date().getTime();
var timeDiff = currentTime - lastActive;
if (timeDiff > 10000) {
//clearInterval(timerId);
}

//I want to do some logic here
// but only on the first iteration of the timer
//how can I do that?

};

$(window).on('mousemove scroll', function (e) {
lastActive = new Date().getTime();

//only restart the timer if its not currently running. How can I do that??
if(resetTimer)
timerId = setInterval(timerFunc , 10000);
});

timerId = setInterval(timerFunc , 10000);

任何 javascript 专家都可以帮我填写吗?如果我太简短,我深表歉意。我会跟进评论中的任何问题。谢谢大家提供的任何提示、链接、技巧等。干杯。 =)

最佳答案

我会为第一个间隔使用一个 bool 变量(这很简单,只需在计时器之后添加它。您在 if(resetTimer) 中也有正确的想法,只需使用计时器即可检查它是否正在运行函数在运行和停止时设置一个全局变量。

关于Javascript 计时器算法帮助,做一次,然后 clearInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11313134/

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