admin管理员组

文章数量:1425815

Does a function like this slow performance or is it considered bad practice?

setInterval(function(){
    if(something){
       do something
    }
}, 100);

Seems to me it would be bad to have something constantly running in the background.

Does a function like this slow performance or is it considered bad practice?

setInterval(function(){
    if(something){
       do something
    }
}, 100);

Seems to me it would be bad to have something constantly running in the background.

Share Improve this question asked Oct 5, 2017 at 22:26 NewCodeManNewCodeMan 2276 silver badges17 bronze badges 2
  • If there has to be sth run regularily, what other way would be there?? – Jonas Wilms Commented Oct 5, 2017 at 22:34
  • I don't know. I'm a beginner :o) but I will learn more – NewCodeMan Commented Oct 5, 2017 at 22:36
Add a ment  | 

1 Answer 1

Reset to default 8

It is running a set of instructions every so many milliseconds, so it can "slow things down," depending on how intensive the instructions are and how many other instructions the user is running from other processes or on your webpage.

It is bad practice if not necessary. If your app is an app that does a thing every so many milliseconds, then that is one way to acplish it. If you are resigning to use it because you just can't figure out the correct way of doing your task, you should spend time to find and implement the correct way of doing your task, instead of harassing the user's CPU cycles.

本文标签: javascriptIs it bad to have setInterval running all the timeStack Overflow