-1

Is the following usage of clearInterval() and setInterval() bad in any way? If so, in what situation?

var myInterval;

if (spaceData.pushed == 1) {
    clearInterval(myInterval);
    [...some code...]
}
else {
    myInterval = setInterval(function() {
        [...some other code...]
    },1000/60);
}

What the code does is that it executes some code when the spacebar har been pushed and some other code when the spacebar has been released.

Rorro
  • 1
  • 3
  • From your description its not clear why you need to use intervals at all – Hunter Aug 09 '16 at 19:59
  • 1
    Well, you're clearing the interval in either branch of the `if`, so you could just pull it before it. Unless there is code missing that is required to be run before the statement, that is. But other than that, with the current information, I don't think there is a problem. Can you elaborate on why you think it might be an issue? – VLAZ Aug 09 '16 at 20:00
  • `func()` should be `function()` – TimoStaudinger Aug 09 '16 at 20:02
  • @Rorro, are you looking for something like this - http://codepen.io/nagasai/pen/grQNxq – Naga Sai A Aug 09 '16 at 20:11
  • I am using the intervals to send data from a node.js server to the client. I am using this way as a solution to prevent simple console editing of variables. @Hunter. – Rorro Aug 09 '16 at 20:12
  • there is a syntax error for function keyword , other than that , everything looks fine – Naga Sai A Aug 09 '16 at 20:12
  • @Vld is there any information of the interval saving after the clearInterval runs that must be removed? or does clearInterval leave no trace of the setInterval? – Rorro Aug 09 '16 at 20:17
  • @TimoSta fixed. Thanks. – Rorro Aug 09 '16 at 20:18
  • @Rorro `setInterval` schedules something to be called each tick and you specify how often it ticks. So, say, if you want to run a function every 10 seconds, it'd be called every 10 seconds. If you `clearInterval` between the two ticks, the next doesn't happen - e.g., you start a timer at T0, function is called at T1 (+10s), gets called again at T2 (+20s), you clear at 25s in, so tick 3 doesn't happen. – VLAZ Aug 09 '16 at 20:26
  • I just wanted to mention one more thing - not sure for your use case, but sometimes you may just want to leave the interval going but suppress the execution of the main logic based on a factor. So your function would look something like `if (shouldRun) { /* code */}` and you can control the `shouldRun` variable from elsewhere. Again, it depends on your needs - it's normally merely an alternative, unless you, say, have some internal state to the periodically executing that you want to preserve. – VLAZ Aug 09 '16 at 20:30

1 Answers1

0

@Rorro are you looking for something like this (I have used @Naga Sai A's example):

var myInterval = setInterval(function() {
  if (spaceData.pushed == 1) {
    clearInterval(myInterval);
  }
  else{
    document.getElementById('demo').innerHTML= "x : " +counter++;
    console.log("x");
  }
},1000/60);

Set the interval and inside the interval handler check for the value, if the value is what is expected then clear the interval else keep logging or updating.

kdpatil
  • 23
  • 4