Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

13
  • 6
    i see. must it always be a local variable? in my case, i set it as global because i have outer function that will call clearInterval... and also, i have 2 setInterval at the time being and they are clashing :/ Commented May 17, 2013 at 1:15
  • i have a question here, will it stuck at the point of clearInterval if the setInterval has stopped somewhere else / hasnt start at all? Commented May 17, 2013 at 1:21
  • 11
    The fact that this approach works boggles my mind. We are referencing a variable in the variable definition itself. How does this work if we are still defining what 'timer' is and then calling it as an argument to clearInterval? Commented Oct 25, 2019 at 13:26
  • 3
    @TeeJ Timers, ajax, and other async operations, etc. will start to make sense if you know how the event loop works. In this case, setInterval simply returns a timer id to timer. When at least 200ms has passed, the callback is called. By that time, you do have a timer variable with a value. Commented Oct 25, 2019 at 13:37
  • 8
    The answer is good but this comment //this will still run after clearing is ambiguous the correct comment would be : //This run ONLY ONE TIME and stop Commented Aug 24, 2020 at 13:52