Introduction to Asynchronous JavaScript --- setInterval()

Hi everybody, in the chapter “Introduction to Asynchronous JavaScript — setInterval()” there is this example:
setInterval(() => {
alert(‘Are you paying attention???’)
}, 300000)

and this very funny comment underneath :
Note: Please don’t actually do this in your apps, thank you.
Why this? Why it should not be used? Just because is disturbing or there are other reasons?

Thanks everyone

Mostly because it would be annoying to have that alert appear every 300 seconds, but also because it’s kind of an artificial bit of code that is used as a teaching device. Asynchronyous coding is difficult to teach using real examples because it requires sending requests to external services that the teaching platform may have no control over. So setInterval() is often used to simulate this cycle of send a request, wait for a response, and do such and such an action after response received.

That way you can focus on learning how to work with code that doesn’t execute straight away without needing to worry about making external API calls over the internet. In a real app, you’d be sending HTTP requests and handling all the logic with promises or async/await, but Codecademy is just trying to focus on one thing at a time in order to not overwhelm learners.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.