使用setTimeout()可以在指定的时间启动一个函数:

setTimeout(function, 60000);

但是如果我想多次启动这个函数呢?每当一个时间间隔过去时,我都希望执行函数(假设每60秒执行一次)。


当前回答

在jQuery中,你可以这样做。

函数random_no () { var跑= math . random (); jQuery (# random_no_container) . html(跑); } setinterval(函数(){ ///调用你的函数 random_no (); }, 6000);//将Interval改为test。例如:5000秒 < script src = " https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js " > < /脚本> < div id = " random_no_container " > 你好。在这里你可以看到每6秒后的随机数 < / div >

其他回答

使用

setInterval(function, 60000);

编辑:(如果你想在时钟启动后停止它)

脚本部分

<script>
var int=self.setInterval(function, 60000);
</script>

及HTML代码

<!-- Stop Button -->
<a href="#" onclick="window.clearInterval(int);return false;">Stop</a>

一个订阅setInterval()并使用clearInterval()来停止永久循环的好例子:

function myTimer() {

}

var timer = setInterval(myTimer, 5000);

调用这一行来停止循环:

clearInterval(timer);

使用窗口。setInterval(函数、时间)。

函数随机(数){ return Math.floor(Math.random() * (number+1)); } setInterval(() => { const rndCol = ' rgb(' +随机(255 ) + ',' + 随机(255 ) + ',' + 随机(255 ) + ')';// rgb值(0 - 255 0 - 255 0 - 255) document.body.style.backgroundColor = rndCol; }, 1000); < script src = " . js " > < /脚本> 它每1秒改变背景颜色(在JS中写为1000)

在这里,我们安慰自然数字0到......n(下一个数字每60秒在控制台打印一次),使用setInterval()

var count = 0;
function abc(){
    count ++;
    console.log(count);
}
setInterval(abc,60*1000);