Too many setTimeout () s good?

I mainly use Javascript for my scripts. I have several scripts that perform several tasks every 1 second, and some every 0.02 seconds, etc. My intervals perform tasks like checking ifs, doing some innerHTML, a little animation, etc. Now I have 4, but I think this is an increase in the future; perhaps I will control myself in less than 10. Although it does not lag behind on my computer.

  • Is this usually good for a site?
  • Since the client side will clearly not have problems connecting to the Internet, right?
  • Is this a bad practice, or is it not a big problem?

Also, I have a question for jQuery. There are things that regular Javascript and jQuery can do the same (e.g. innerHTML and .html ()), right? Given this situation that I should use, jQuery or Javascript?

Thanks.

+4
source share
5 answers

Are there too many setTimeout () s good? : you have already given your answer; too much is not very good, no matter what you say.

This will usually be good for the site . Probably not, remember that you don’t know the processor / browser combojube of your users, so you need to create your site so that it is still enjoyable for people with slower computers than you. The loggie / unresponsive site is the first from which users will run.

Since there will clearly be no problems connecting to the Internet on the client side, right? if you are not performing network requests as part of timeouts. Ajax calls every second, rarely a good idea.

Is this bad practice or is it not a problem at all? Bad architecture is bad practice. If you want to follow best practices, check out design patterns.

Also, I have a question for jQuery. There are things that regular Javascript and jQuery can do in a similar way (e.g. innerHTML and .html ()), right? Given this situation that I should use, jQuery or Javascript? Using jQuery gives you convenient cross-browser compatibility. In most cases, for non-experts using a library such as jQuery, it is better than not using it.

+6
source

Is this usually good for a site? Most other respondents say no, but they cannot make this expression without additional information. This solution is specific to your site. It can be great. Do not speculate about performance. Measure things. Fix performance issues where they really exist, and not where your gut tells you that they exist.

Since there will obviously be no problems connecting to the Internet on the client side, right? Right.

Is this bad practice or is it not a problem at all? There are many tasks that are best accomplished with a timeout callback. If you need to do something periodically or after a certain time, then setTimeout not only wise to use, it is specifically designed for these tasks.

+3
source

For your first question, this is more likely the case of what you do in every callback that is called after a timeout. Not knowing that it is difficult to give a clearer answer.

But remember that javascript is single-threaded in the browser, so if you regularly perform expensive operations, it will not work well. If you want to make an animation with this, I strongly recommend that you first consider one of the existing libraries (dojo, jquery, mootools, etc.) And just try to implement this yourself if you find them insufficient. It took a lot of work to make those libraries, and you are unlikely to do better with the first blow.

Second question: Again, I would use a library. For a simple innerHTML, there is unlikely to be much difference, but in the general case, a library like jQuery or Dojo will help cover the differences between the DOM in different browsers, catch the nasty edges that you don't know (or want to know) and are probably best tested and supported than native code. Get it by working their way, then optimize with your own vanilla JS if / when you need to.

+2
source

1) In general, it’s good: no, in general, it creates a processor load, which slows down the browser. But this will be influenced by the actual code rather than the timeout itself. But a modern browser does not have problems with 10 timers ...;)

2) Yes, no problems with the timeout.

3) No, this is the only way to do asynchronous processing. so this is actually not a problem;)

4) The difference with innerHTML and .html () (and other jQuery functions) is that jQuery has better cross-browser implementations. He knows the quirks of certain browsers and is able to get around them. For simple tasks, simple JavaScript is ideal (and cheaper in bandwidth and processing power). But for more complex scripts, and as soon as you decide to use jQuery, why not use it anyway .;)

+1
source

Go with jQuery - write less, do more

You can have so many functions, effects, plugins that can help you save time from coding.

In any case, when you design a website with javascript or jquery effects, you will expect the user to have an updated browser and system.

your functionality will not work, i.e. 5.:)

Thus, adding a timeout and js functions will not affect performance in accordance with my understanding.

http://api.jquery.com/delay/

http://api.jquery.com/toggle/

http://api.jquery.com/html/

Go with the jquery api functions and create your own .. instead of usng plugins.

0
source

Source: https://habr.com/ru/post/1344105/


All Articles