JavaScript:获取代码以便每分钟运行一次 [英] JavaScript: get code to run every minute

查看:109
本文介绍了JavaScript:获取代码以便每分钟运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让一些JS代码每60秒执行一次?我认为可能有一个循环,但是有一个更好的解决方案吗? JQuery欢迎,一如既往。

解决方案

使用 setInterval
$ b

  setInterval(function (){
//你的代码在这里...
},60 * 1000); // 60 * 1000 milsec

该函数返回一个id,您可以使用 clearInterval

  var timerID = setInterval(function(){
// your code goes here ...
},60 * 1000);

clearInterval(timerID); //清除setInterval并不再运行。






姐妹功能是 setTimeout / clearTimeout 查找它们。




如果您想在页面init上运行一个函数,然后在60秒后,120秒后运行一个函数:

 函数fn60sec(){
//每60秒运行一次并在init上运行。
}
fn60sec();
setInterval(fn60sec,60 * 1000);


Is there a way to make some JS code be executed every 60 seconds? I'm thinking it might be possible with a while loop, but is there a neater solution? JQuery welcome, as always.

解决方案

Using setInterval:

setInterval(function() {
    // your code goes here...
}, 60 * 1000); // 60 * 1000 milsec

The function returns an id you can clear your interval with clearInterval:

var timerID = setInterval(function() {
    // your code goes here...
}, 60 * 1000); 

clearInterval(timerID); // The setInterval it cleared and doesn't run anymore.


A "sister" function is setTimeout/clearTimeout look them up.


If you want to run a function on page init and then 60 seconds after, 120 sec after, ...:

function fn60sec() {
    // runs every 60 sec and runs on init.
}
fn60sec();
setInterval(fn60sec, 60*1000);

这篇关于JavaScript:获取代码以便每分钟运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆