timedLoop vs setInterval

Hi!

I just want to double check if timedLoop is the same as setInterval, with the parameters reversed? If so, I like the change, as it seems more intuitive to the students. If not, what am I missing?

Thanks!

1 Like

Which lesson(s) are you referring to? Are you talking about setInterval vs setTimeout?

Thanks for your reply, bhatnagars!

Actually, I was referring to App Lab overall. The setInterval block is gone, and there is now a timedLoop block.I figured the CSP Unit 5 was the best place to post, since hopefully there will be some teachers here using App Lab right now.

I’ve been playing with it, and it seems to be the same, but I wanted to double check in case I am missing something.

Hi.

Short version: setInterval, like many other native JavaScript functions, still works it’s just not in the toolbox. timedLoop was developed as a simplifying API for setInterval with two important differences.

  1. yes, the params are reversed because the setInterval way is annoying :slight_smile:
  2. the timer interval key is stored globally behind the scenes so that you don’t have to keep track of it and you can call stopTimedLoop() from anywhere you like at any point and it will halt.**

That said timedLoop does still return the interval key, so you can use it like setInterval/clearInterval just with the params reversed.

We hope this is more intuitive and actually leads to fewer errors in users’ code. setInterval can lead to some really dastardly, basically-impossible-to-debug, problems.

And yes, we need to get documentation up about this pronto!

Thanks,

-Baker

FYI here is a simple example. It doesn’t show all the features but at least shows how to use timedLoop and stopTimedLoop.

var count = 10;

timedLoop(1000, function() {  // call the function once per second

  console.log(count);         // display count and decrement
  count--;
  
  if(count == 0){              // stop the timer once we hit zero
    console.log("Blast off!");
    stopTimedLoop();
  }
});
1 Like