每60秒调用一次函数

时间:2010-06-29 07:43:19

标签: javascript function timer setinterval

使用setTimeout()可以在指定时间启动功能:

setTimeout(function, 60000);

但是,如果我想多次启动该功能怎么办?每次时间间隔过去,我都想执行该功能(每60秒,让我们说)。

15 个答案:

答案 0 :(得分:331)

如果您不关心timer中的代码可能需要的时间超过您的时间间隔,请使用setInterval()

setInterval(function, delay)

一次又一次地激活作为第一个参数传入的函数。

更好的方法是使用setTimeoutself-executing anonymous函数:

(function(){
    // do some stuff
    setTimeout(arguments.callee, 60000);
})();

保证在执行代码之前不会进行下一次调用。我在此示例中使用arguments.callee作为函数引用。这是一个更好的方法,为函数命名并在setTimeout内调用,因为在ecmascript 5中不推荐使用arguments.callee

答案 1 :(得分:60)

使用

setInterval(function, 60000);

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

脚本部分

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

和HTML代码

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

答案 2 :(得分:22)

更好地利用jAndyanswer来实现一个轮询函数,该函数每{{}}}轮询一次,并在interval秒后结束。

timeout

<强>更新

根据评论,更新它以传递函数停止轮询的能力:

function pollFunc(fn, timeout, interval) {
    var startTime = (new Date()).getTime();
    interval = interval || 1000;

    (function p() {
        fn();
        if (((new Date).getTime() - startTime ) <= timeout)  {
            setTimeout(p, interval);
        }
    })();
}

pollFunc(sendHeartBeat, 60000, 1000);

答案 3 :(得分:12)

在jQuery中你可以这样做。

&#13;
&#13;
function random_no(){
     var ran=Math.random();
     jQuery('#random_no_container').html(ran);
}
           
window.setInterval(function(){
       /// call your function here
      random_no();
}, 6000);  // Change Interval here to test. For eg: 5000 for 5 sec
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="random_no_container">
      Hello. Here you can see random numbers after every 6 sec
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:8)

setInterval(fn,time)

是你追求的方法。

答案 5 :(得分:7)

您可以在函数末尾调用setTimeout。这会将它再次添加到事件队列中。您可以使用任何类型的逻辑来改变延迟值。例如,

function multiStep() {
  // do some work here
  blah_blah_whatever();
  var newtime = 60000;
  if (!requestStop) {
    setTimeout(multiStep, newtime);
  }
}

答案 6 :(得分:6)

答案 7 :(得分:3)

每2秒钟连续调用Javascript函数10秒钟。

var intervalPromise;
$scope.startTimer = function(fn, delay, timeoutTime) {
    intervalPromise = $interval(function() {
        fn();
        var currentTime = new Date().getTime() - $scope.startTime;
        if (currentTime > timeoutTime){
            $interval.cancel(intervalPromise);
          }                  
    }, delay);
};

$scope.startTimer(hello, 2000, 10000);

hello(){
  console.log("hello");
}

答案 8 :(得分:2)

function random(number) {
  return Math.floor(Math.random() * (number+1));
}
setInterval(() => {
    const rndCol = 'rgb(' + random(255) + ',' + random(255) + ',' + random(255) + ')';//rgb value (0-255,0-255,0-255)
    document.body.style.backgroundColor = rndCol;   
}, 1000);
<script src="test.js"></script>
it changes background color in every 1 second (written as 1000 in JS)

答案 9 :(得分:1)

// example:
// checkEach(1000, () => {
//   if(!canIDoWorkNow()) {
//     return true // try again after 1 second
//   }
//
//   doWork()
// })
export function checkEach(milliseconds, fn) {
  const timer = setInterval(
    () => {
      try {
        const retry = fn()

        if (retry !== true) {
          clearInterval(timer)
        }
      } catch (e) {
        clearInterval(timer)

        throw e
      }
    },
    milliseconds
  )
}

答案 10 :(得分:0)

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

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

答案 11 :(得分:0)

一个很好的示例,其中可以订阅setInterval()并使用clearInterval()来停止永远的循环:

4294967295

调用此行以停止循环:

ffffffff

答案 12 :(得分:0)

我喜欢调用一个包含循环函数的函数,该循环函数定期调用自身的 setTimeout

function timer(interval = 1000) {
  function loop(count = 1) {
    console.log(count);
    setTimeout(loop, interval, ++count);
  }
  loop();
}

timer();

答案 13 :(得分:-1)

有两种方式可以调用 -

  1. setInterval(function (){ functionName();}, 60000);

  2. setInterval(functionName, 60000);

  3. 以上功能将每60秒调用一次。

答案 14 :(得分:-1)

(function(sec){
  setInterval(function(){
    // your function here
  }, sec * 1000);
})(60);
相关问题