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 trial

JavaScript Callback Functions in JavaScript Callbacks with Timers Using a One-Off Timer with setTimeout

1 Answer

Steven Parker
Steven Parker
231,248 Points

You 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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

Hey 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
Steven Parker
231,248 Points

The 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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

Steven 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
Steven Parker
231,248 Points

If 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.