Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialkaran Badhwar
Web Development Techdegree Graduate 18,135 PointsTimeoutID
What is the TimeoutId
?
if I console.log(timeoutId);
, it always prints 1 and how that helps us to cancel the setTimeout function?
1 Answer
Steven Parker
231,275 PointsYou can have more than one timeout active at the same time. If you do, each one will have a different TimeoutID.
The TimeoutID must be passed as an argument when you call clearTimeout to cancel the timer before the time elapses.. But if your program doesn't ever cancel a timer, is isn't needed.
For more details, see the MDN pages for setTimeout and clearTimeout.
karan Badhwar
Web Development Techdegree Graduate 18,135 Pointskaran Badhwar
Web Development Techdegree Graduate 18,135 PointsHey Steven Parker, I tried but I did not understand that what is what does setTimeout or setInterval return, as in what that they return that we are passing that as an argument? What is the value?
Steven Parker
231,275 PointsSteven Parker
231,275 PointsThe TimeoutID is simply an integer, and as you already noticed, it is often just the value
1
. What's important is that JavaScript internally recognizes it as identifying the specific timer you want to clear.karan Badhwar
Web Development Techdegree Graduate 18,135 Pointskaran Badhwar
Web Development Techdegree Graduate 18,135 PointsSteven Parker, so you mean to say it alerts the JavaScript internally to clear out the timer function and we can treat 1 as a signal to go for Clear? Is my understanding of it right?
Steven Parker
231,275 PointsSteven Parker
231,275 PointsIf the delay time is 0, you won't be able to clear the timer before the event starts (so you won't need the TimerID). And never assume that the value is 1 .. always store and use whatever setTimeout returns.
karan Badhwar
Web Development Techdegree Graduate 18,135 Pointskaran Badhwar
Web Development Techdegree Graduate 18,135 PointsOk ok Thanks a lot Steven Parker for the help