Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is having too many setTimeout()s good?

I'm using mostly Javascript on my scripting. I have some scripts which do several tasks every 1 second, and some every 0.02 seconds, etc. My intervals do tasks like checking of ifs, doing some innerHTML, little animation, etc. Now I have 4, but I think it would increase in the future; maybe I'll control myself to less than 10. Though it doesn't lag at all in my computer.

  1. Generally, will it be good for a site?
  2. Since it's client-side obviously there won't be any issues with internet connection, right?
  3. Is it a bad practice, or is it not much of an issue at all?

Also, I have a question for jQuery. There are things that normal Javascript and jQuery can do similarly (like innerHTML and .html()), right? Given this situation, which should I prefer to use, jQuery or Javascript?

Thank you.

like image 836
ton Avatar asked Mar 17 '11 14:03

ton


People also ask

Does setTimeout cause performance issues?

No significant effect at all, setTimeout runs in an event loop, it doesn't block or harm execution.

Is there a limit for setTimeout?

Maximum delay value 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.

How many times setTimeout executed?

2 Answers. Save this answer. Show activity on this post. setTimeout will only execute once.

Should setTimeout be cleared?

No, setTimeout stops running after 1 call. In order to stop setInterval however, you must use clearInterval . If you create an endless loop of setTimeout then clearTimeout could be used.


2 Answers

Is having too many setTimeout()s good?: You already gave your self the answer too many is not good, no matter what you talk about.

Generally will it be good for the site: Probably not, remember you don't know the cpu/browser combo of your users, so you should build your site so it will still be nice to use for people who have slower computers than you. A laggy/unresponsive site is the first one users will flee from.

Since it's client-side obviously there won't be any issues with internet connection, right? right, unless you do network requests as part of the timeouts. Ajax calls every second is rarely a good idea.

Is it a bad practice, or is it not much of an issue at all? poor architecture is bad practice. If you wish to follow the best practices, read up on design patterns.

Also, I have a question for jQuery. There are things that normal Javascript and jQuery can do similarly (like innerHTML and .html()), right? Given this situation, which should I prefer to use, jQuery or Javascript? Using jQuery gives you convinient crossbrowser compatibility. In most cases for non-experts using a library like jQuery is better than not using it.

like image 137
Martin Jespersen Avatar answered Oct 05 '22 05:10

Martin Jespersen


Generally, will it be good for a site? Most of the other answerers are saying no, but they can't possibly make this claim without more information. This decision is specific to your site. It might be perfectly fine. Don't make guesses about performance. Measure things. Fix performance issues where they really exist, not where your gut tells you they exist.

Since it's client-side obviously there won't be any issues with internet connection, right? Correct.

Is it a bad practice, or is it not much of an issue at all? There are lots of tasks that are best performed with a timeout callback. If you need to do something periodically, or after a certain amount of time has passed, then setTimeout is not only reasonable to use, it's specifically designed for those tasks.

like image 20
Wayne Avatar answered Oct 05 '22 04:10

Wayne