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 Random Quote Generator

Rob Williams
Rob Williams
18,708 Points

setInterval question on the random quote generator project


I'm working on the random quote generator project and can't quite get the setInterval() to work. It loads a new quote ok every 10 seconds like it should, but clicking the button doesn't reset the countdown like I thought it would. Can anyone help?

e.g. if i click the 'show a new quote' button, it will bring up a new quote and start counting down the 10 seconds until automatically loading a new one. If I wait 5 seconds and click the button again, the next quote will only show for 5 seconds before the next loads because the countdown started on the first click.....hope this is making sense!



1 Answer

Steven Parker
Steven Parker
229,657 Points

Be aware that only other Techdegree students can view your project link. And it would help greatly if you included your code, without it this is a bit of a guess:

:point_right: You probably forgot to clear the current interval.

Based on your description, it sounds like you need to end the old interval and start a new one. Did you retain the interval ID returned by your setInterval() call? When the button is clicked again, you can use it to call clearInterval() and then start fresh with a new setInterval() (and be sure to retain the ID again).