gpt4 book ai didi

javascript - 每 5 秒执行一些操作以及停止它的代码。 (JQuery)

转载 作者:行者123 更新时间:2023-12-03 21:43:54 25 4
gpt4 key购买 nike

如何每 5 秒重复一次函数 doSomething()

我还需要代码让它停止这样做。

以及用于动态调整频率的代码。

最佳答案

setTimeout() 只会启动该命令一次。在这种情况下,setInterval() 是你的 friend 。

var iFrequency = 5000; // expressed in miliseconds
var myInterval = 0;

// STARTS and Resets the loop if any
function startLoop() {
if(myInterval > 0) clearInterval(myInterval); // stop
myInterval = setInterval( "doSomething()", iFrequency ); // run
}

function doSomething()
{
// (do something here)
}

来自代码...

<input type="button" onclick="iFrequency+=1000; startLoop(); return false;" 
value="Add 1 second more to the interval" />

关于javascript - 每 5 秒执行一些操作以及停止它的代码。 (JQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1542280/

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