Javascript中的setInterval函数 [英] setInterval function in Javascript

查看:94
本文介绍了Javascript中的setInterval函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在一个特定条件下启动setInterval()并在该条件完成后停止它????

在我的程序中,一个函数在5秒后执行。当该函数成功执行时,我想停止setinterval函数.. ???

How to start setInterval() at one perticular condition and stop it after that condition get done????
In my program one function get executed after 5 seconds. when that function successfuly get executed then i want to stop setinterval function.. ???

推荐答案

试试这个:

Try this:
var intervalID;
function doSomething()
{
    if ( condition )
    {
        clearInterval(intervalID);
    }
}
intervalID = setInterval(doSomething, 5000);


你好RHL,



最好使用 setTimeout()而不是 setInterval ()因为它更符合你的要求。



这是你怎么做的

Hi RHL,

Its better to use setTimeout() instead of setInterval() since it better suits your requirement.

This is how you can do it
var timer = setTimeout("yourFunc()", 5000); //here it will execute after 5 secs

function yourFunc()
{
/*
your code
*/
if(timer)
clearTimeout(timer); //Here your timer will get stopped.
}



注意:如果你使用setInterval()然后你需要使用clearInterval()函数来停止计时器

希望这可以帮助你位。



问候,

RK


Note: If you use setInterval() then you need to use clearInterval() function to stop the timer
Hope this helps you a bit.

Regards,
RK


这篇关于Javascript中的setInterval函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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