Jump to content

clearInterval() question


Recommended Posts

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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...