Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Random Quote Generator

Rob Williams
Rob Williams
18,708 Points

setInterval question on the random quote generator project

Hi,

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!

Thanks,

Rob

1 Answer

Steven Parker
Steven Parker
220,378 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).