Jump to content

Understanding setInterval variable value


Recommended Posts

I want to check I understand how this works. If I set an interval and assign it to variable a, then put 'a' into console.log and it comes out as '2', that means it's the second interval running on the page, yes?


I think I'm not managing to cancel intervals in my code so I've been trying to use this method to understand what's going on. One of my intervals keeps coming out as 2 in Firefox, so I thought, that particular one isn't causing a problem...


But then I tested my intervals in Internet Explorer 8, and got these values:






So I wondered if I've gotten the wrong idea about this? There can't possibly be over 2 million intervals running on the page (surely not where there aren't that many in Firefox).


(I could happily never use intervals/timeouts ever, ever again at this point....)

Edited by Lucy
Link to comment
Share on other sites

I suspect that the values of these variables are meaningless. You use one variable per timer and you only use the variable to clear the timer it is associated with. You might set the variable to null when you want to remember that the timer is not running.

Link to comment
Share on other sites

setTimeout() and setInterval() return a positive integer to be used as an identifier for the timer. There's no specification as to what value the number should have aside from being a positive integer. Some browsers may choose to start with 1, while others might decide to use a random number.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...