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
Trevor Johnson
14,427 PointsJS Help with clearing setInterval function
Hi, I am working on a Simon game project. I need to be able to turn the game on and off. When the game is off it needs to totally reset everything, and I have done that for the most part, but sometimes the lights continue to flash and won't stop if I happen to hit stop in the middle of a flash on the second turn or so if there are more flashes after it. To replicate my issue turn the game on. hit start. copy the moves correctly and get to the third round. on the fourth round turn the game off in the middle of the flash on the third round. Then turn it back on and you will see. I have the same problem when I try to hit start in the middle of a flash. The start button needs to act as a reset button that will start a new game without turning the game off and back on.
Start button code starts on line 137 Computer move code starts on line 91 On button line 66 Clear All line 38
Thanks for checking this out I really appreciate it.
1 Answer
Steven Parker
243,201 PointsYou need to keep the interval number where you can access it from the clearAll function.
You could perhaps replace the "moves" variable in pcMove with game.moves, which you would add to the game object. Then you could call "clearInterval(game.moves)" from clearAll, and perhaps also from the "start" button handler.
Trevor Johnson
14,427 PointsThank you that works. The start button still acts weird though. It doesn't register my first click on the first round, but after that it does. my console.log is showing that the move that was meant to be next doesn't get cleared when restart is pressed so it is still expecting 2 clicks. Let's say the current round is 4. Try restarting on the third flash of the 4th round and you should see it. It doesn't seem to do it when it is the last flash.
Steven Parker
243,201 PointsI did notice you still have a few additional opportunities to practice debugging. Give them your best shot and if you get stuck you can always create another question here.
Trevor Johnson
14,427 PointsThank you. I will try and get it
Trevor Johnson
14,427 PointsTrevor Johnson
14,427 PointsI added a console.log(game.pc) on line 101, and that is showing me that my setInterval() starts running continuously once the off button is hit and I don't know what to do about this.