NewCodeMan
NewCodeMan

Reputation: 227

Is it bad to have setInterval running all the time?

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.

Upvotes: 2

Views: 2457

Answers (1)

Charles Stover
Charles Stover

Reputation: 1148

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 accomplish 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.

Upvotes: 8

Related Questions