如何每3秒钟调用一次15秒的功能? [英] How can I call a function every 3 seconds for 15 seconds?

查看:71
本文介绍了如何每3秒钟调用一次15秒的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何每隔3秒调用一次jQuery函数?

How do I call a jQuery function every 3 seconds?

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

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

    //do stuff...
})

我正在尝试运行该代码15秒。

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

推荐答案

没有答案到目前为止考虑到它只想发生15秒然后停止......

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

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

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

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秒的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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