JavaScript timer function not working properly in Spotify application

I’m struggling with getting a timer to work correctly using JavaScript timing functions. When I write timer = setInterval(myFunction(), 1000); it only executes myFunction() one time instead of repeating every second like it should. Then I attempted using timer = setInterval("myFunction()", 1000); which seems to trigger something because I get Uncaught ReferenceError: myFunction is not defined appearing every second. Can someone explain what’s going wrong here?

The issue you’re encountering stems from how you’re calling the function in the setInterval method. By using setInterval(myFunction(), 1000), you’re executing myFunction right away instead of passing the function itself to setInterval, which is why it runs only once. You should write it as setInterval(myFunction, 1000) to pass the function reference correctly. The string method also triggers a global scope evaluation which may not recognize myFunction. Trust me, I’ve faced similar frustrations while coding. Remember to utilize clearInterval(timer) once your timer is no longer needed to avoid potential memory issues.

yeah, totally get it! that happens a lot. just remember, with setInterval(myFunction()), you’re calling it instantly. you wanna go with setInterval(myFunction, 1000) so it works like it should. the string method just messes up scope. good luck!