Whats the difference between setInterval and setTimeout?

272 viewsSoftware & Technologyjavascript
0

In standard Javascript the setInterval and setTimeout functions appear to do the same thing and accept the same arguments. What is the difference between the 2 of them? When should one be used over the other?

Edited question
0

They are almost the exact same thing bar one small but very significant difference. Both methods accept a time and a function to perform when the time is run. If you use setTimeout it will run once and be done. With setInterval it will run again and again. Mixing this up can be dangerous on a website that gets a lot of traffic as it could hurt your websites performance or it could bring the users page to a crawl. Always default to using setTimeout to keep it safe.

setTimeout = runs once when the time span provided is reached

setInterval = runs over and over based on the time span provided.

Changed status to publish