如何每3秒调用一个函数,持续15秒?

时间:2022-10-28 01:24:23

How do I call a jQuery function every 3 seconds?

我如何每3秒调用一个jQuery函数?

$(document).ready(function ()
{
    //do stuff...

    $('post').each(function()
    {
        //do stuff...
    })

    //do stuff...
})

I'm trying to run that code for a period of 15 seconds.

我试着运行这段代码15秒。

5 个解决方案

#1


11  

None of the answers so far take into account that it only wants to happen for 15 seconds and then stop...

到目前为止,没有一个答案考虑到它只希望发生15秒钟,然后停止……

$(function() {
    var intervalID = setInterval(function() {
        //  Do whatever in here that happens every 3 seconds
    }, 3000);
    setTimeout(function() {
        clearInterval(intervalID);
    }, 18000);
});

This creates an interval (every 3 seconds) that runs whatever code you put in the function. After 15 seconds the interval is destroyed (there is an initial 3 second delay, hence the 18 second overall runtime).

这将创建一个间隔(每3秒),该间隔运行您在函数中输入的任何代码。15秒后,时间间隔被破坏(初始延迟为3秒,因此整个运行时间为18秒)。

#2


1  

You can use setTimeout to run a function after X milliseconds have passed.

您可以使用setTimeout在X毫秒后运行一个函数。

var timeout = setTimeout(function(){
    $('post').each(function(){
        //do stuff...
    });
}, 3000);

Or, setInterval to run a function every X milliseconds.

或者,setInterval:每X毫秒运行一个函数。

var interval = setInterval(function(){
    $('post').each(function(){
        //do stuff...
    });
}, 3000);

setTimeout and setInterval return IDs, these can be used to clear the timeout/interval using clearTimeout or clearInterval.

setTimeout和setInterval返回id,可以使用clearTimeout或clearInterval清除超时/间隔。

#3


0  

You could use the setTimeout method also, which supports things like cancelling the timer.

您也可以使用setTimeout方法,它支持取消计时器等功能。

See: http://msdn.microsoft.com/en-us/library/ie/ms536753(v=vs.85).aspx

参见:http://msdn.microsoft.com/en-us/library/ie/ms536753(v = vs.85). aspx

#4


0  

setInterval(function() {
      // Do something every 3 seconds
}, 3000);

#5


0  

Use the setInterval function.

使用setInterval函数。

var doPost = function() {
  $('post').each(function() { 
    ...
  });
};
setInterval(function() { doPost(); }, 3000);

#1


11  

None of the answers so far take into account that it only wants to happen for 15 seconds and then stop...

到目前为止,没有一个答案考虑到它只希望发生15秒钟,然后停止……

$(function() {
    var intervalID = setInterval(function() {
        //  Do whatever in here that happens every 3 seconds
    }, 3000);
    setTimeout(function() {
        clearInterval(intervalID);
    }, 18000);
});

This creates an interval (every 3 seconds) that runs whatever code you put in the function. After 15 seconds the interval is destroyed (there is an initial 3 second delay, hence the 18 second overall runtime).

这将创建一个间隔(每3秒),该间隔运行您在函数中输入的任何代码。15秒后,时间间隔被破坏(初始延迟为3秒,因此整个运行时间为18秒)。

#2


1  

You can use setTimeout to run a function after X milliseconds have passed.

您可以使用setTimeout在X毫秒后运行一个函数。

var timeout = setTimeout(function(){
    $('post').each(function(){
        //do stuff...
    });
}, 3000);

Or, setInterval to run a function every X milliseconds.

或者,setInterval:每X毫秒运行一个函数。

var interval = setInterval(function(){
    $('post').each(function(){
        //do stuff...
    });
}, 3000);

setTimeout and setInterval return IDs, these can be used to clear the timeout/interval using clearTimeout or clearInterval.

setTimeout和setInterval返回id,可以使用clearTimeout或clearInterval清除超时/间隔。

#3


0  

You could use the setTimeout method also, which supports things like cancelling the timer.

您也可以使用setTimeout方法,它支持取消计时器等功能。

See: http://msdn.microsoft.com/en-us/library/ie/ms536753(v=vs.85).aspx

参见:http://msdn.microsoft.com/en-us/library/ie/ms536753(v = vs.85). aspx

#4


0  

setInterval(function() {
      // Do something every 3 seconds
}, 3000);

#5


0  

Use the setInterval function.

使用setInterval函数。

var doPost = function() {
  $('post').each(function() { 
    ...
  });
};
setInterval(function() { doPost(); }, 3000);