Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 7, 2020 07:46 am GMT

setTimeout() - any drawback?

Recently, I had come across a bug that needed auto-refresh after a certain point of time. And setTimeout() had been used to count down to the time and reload the page. Little did I know that setTimeout() came with a price.

As quoted on MDN Web Docs at the very bottom, "Browsers including Internet Explorer, Chrome, Safari, and Firefox store the delay as a 32-bit signed integer internally. This causes an integer overflow when using delays larger than 2,147,483,647 ms (about 24.8 days), resulting in the timeout being executed immediately."

Now, you'll understand what kept happening to the page! (The page kept reloading...)

It is very much true that there is hardly any process that would require this big a delay. You have other options, like reseting timer after certain duration or using setInterval().

I would prefer setInterval(), when there are simple operations in the callback function and when you know what is the maximum time that function is going to take to execute. If you aren't aware of the maximum time, the event queue will keep piling up forever as your code's activity lags behind the actual system time.

Choose wise! Don't forget to clear the timers when your job is done!


Original Link: https://dev.to/divyajyotiuk/settimeout-any-drawback-47ed

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To