Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.. ???
Posted
Updated 27-Dec-13 22:45pm
v2

Try this:
JavaScript
var intervalID;
function doSomething()
{
    if ( condition )
    {
        clearInterval(intervalID);
    }
}
intervalID = setInterval(doSomething, 5000);
 
Share this answer
 
v2
Hi RHL,

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

This is how you can do it
JavaScript
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.
}

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

Regards,
RK
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900