will71110 Posted December 13, 2012 Share Posted December 13, 2012 Quick question about the clearInterval() function. How can I detect by variable that the function has stopped and cleared? In my examples, how can I use var repeatID to detect this outcome? The first part works when it's running, but I cant get this to start the function back up again. function example{if (repeatID){clearInterval(repeatID)}else{sample()}}function sample(){repeatID = setInterval(function() {...}, 5000);} Link to comment Share on other sites More sharing options...
will71110 Posted December 13, 2012 Author Share Posted December 13, 2012 Ok I may have figured this out, but is this the best way to do this? I know repeatID has to contain something after the clearInterval, but I cant seem to figure out what. if (repeatID){clearInterval(repeatID)repeatID = null}else{sample()}}function sample(){repeatID = setInterval(function() {...}, 5000);} Link to comment Share on other sites More sharing options...
justsomeguy Posted December 13, 2012 Share Posted December 13, 2012 repeatID will probably just be a number, it doesn't mean anything to you other than to identify which interval you want to clear. The value doesn't change after the function runs or completes. I'm not sure what exactly you're asking for, but if it means setting the interval ID to a value of false or null or something that you can test to see if the interval is still running then that will work fine. Link to comment Share on other sites More sharing options...
will71110 Posted December 14, 2012 Author Share Posted December 14, 2012 I was trying to see what the variable repeatID would be after it was ran through the clearinterval(). When I do clearinterval(repeatID), what does repeatID equal now so I can use it to test with a If Statement. Forcing it to be null works but I like to keep my scripts as small as possible. Actually after looking over my code again, repeadID is not an standard variable, isn't it? I guess the way I made this is the best way to accomplish the script. Link to comment Share on other sites More sharing options...
Ingolme Posted December 14, 2012 Share Posted December 14, 2012 repeatID doesn't change when clearInterval() is called. repeatID just tells the system which timer to stop, but the variable itself doesn't have any significant information so it doesn't need to be changed. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now