I assume the interval
you're passing into clearInterval
is meant to be something
.
Your second example will never fire your timer, because you clear the timer immediately after setting it. You're also passing an argument (7000
) into clearInterval
that won't get used (clearInterval
only accepts one argument).
Your first example is right provided that you want to clear the repeated timer at the point where you're calling clearInterval
from within the handler. Presumably that's in an if
or similar, because if you want a one-off timed callback you'd use setTimeout
, not setInterval
.
EDIT:
For #2, I meant setTimeout() instead of clearInterval().Has been changed.
That completely changes the question. No, that's not correct. setInterval
schedules the function to be called repeatedly on the interval you give it, you don't pass its return value into setTimeout
.