Pain Posted February 19, 2013 Share Posted February 19, 2013 Hello. I am in need of some help:) I know how to set up an interval, but now i need to clear it. So i thought u guys could help me out:) I set my interval like this setInterval(function(){ loadUpdateLevelPercentage(); }, 10000); How can i clear it after 40 seconds? Quote Link to comment https://forums.phpfreaks.com/topic/274687-clearinterval/ Share on other sites More sharing options...
kicken Posted February 19, 2013 Share Posted February 19, 2013 with clearInterval. Quote Link to comment https://forums.phpfreaks.com/topic/274687-clearinterval/#findComment-1413400 Share on other sites More sharing options...
Psycho Posted February 19, 2013 Share Posted February 19, 2013 @kicken: I think the issue is in 'how' to implement that after 40 seconds. Well, there are a few options. This is the easiest in my opinion is this: You apparently only want it to run 4 times since each interval is set to 10 seconds. So, set a global counter that is used inside loadUpdateLevelPercentage(). Then at the end of each iteration of the function, increment the counter and do a check to see how many times it has executed. If >= 4 then use clearInterval() Quote Link to comment https://forums.phpfreaks.com/topic/274687-clearinterval/#findComment-1413401 Share on other sites More sharing options...
kicken Posted February 19, 2013 Share Posted February 19, 2013 @Psycho: Aye, guess I didn't read close enough. @Pain: As a side note, there is one caution warning to be aware of with setInterval. If it's possible that the time needed to run your code would be longer than the delay, you should use setTimeout instead to prevent overlapping / queued up calls to the function. With a 10-sec delay that probably isn't an issue for you, though if you're doing any ajax work in the callback you might consider changing formats. Quote Link to comment https://forums.phpfreaks.com/topic/274687-clearinterval/#findComment-1413410 Share on other sites More sharing options...
Pain Posted February 19, 2013 Author Share Posted February 19, 2013 (edited) Thanks for your help guys. Is this what you had in mind? var i = 0; if(i <= 4) { setInterval(function(){ loadUpdateLevelPercentage(); }, 1000); i++; } Edited February 19, 2013 by Pain Quote Link to comment https://forums.phpfreaks.com/topic/274687-clearinterval/#findComment-1413416 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.