Skip to content
Advertisement

HTML/Javascript slow/crashing on quiz app

I have a problem with my otherwise fully functioning quiz game. There seems to be crashing/slow loading issues after around 5 or 6 (out of 10) questions. Its very strange because if I answer everything within a second (unrealistic but to error check), there is no problem. But as soon as I take a “normal” amount of time to answer questions, things slow down and it eventually crashes/gets stuck.

I have tried removing animations & adjusting the flow of my JS but to no avail. If anyone has any thoughts, they would be highly appreciated!

Here is a link to the site itself: https://louparker.github.io/random-music-quiz/index.html

Here is a link to the repo: https://github.com/louparker/random-music-quiz

JavaScript
JavaScript

Thanks in advance!

Advertisement

Answer

Oookey it turns out you have a really big recursion problem. In your countdown interval you give event listeners to the choices and you clear the interval like a good programmer, but then you have forgot that you are in a forEach. So when later you call your restartInterval function you actually do it four times. I think that you can imagine what happens at the sixth question with the 24 intervals running at the same time.

P.S. when you work with intervals always check that only those are running that you intended to run. A god way of checking is a simple console.log() as you see in the snippet down below.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement