stopTimedLoop()

Category:Control

Stops any running Timed Loops, or a specific one if passed in return value of timedLoop().

Some Timed Loops do not run forever, but need to be stopped at some time (maybe like a countdown, see the first example). stopTimedLoop() allows you to stop timed loops from running, either from within the loop itself or elsewhere in your program.

Examples

Countdown

Stop a timed countdown once it reaches 0

var countdown = 10;
textLabel("countdown", countdown);
timedLoop(1000, function() {
  countdown = countdown - 1;
  setText("countdown", countdown);
  if(countdown === 0) {
    stopTimedLoop();
  }
});

Stop the Presses!

Run a half second timed loop until a button is pressed.

// Run a half second timed loop until a button is pressed.
button("stop", "Stop the timer");
timedLoop(500, function() {
  console.log("Timer code ran!");
});
onEvent("stop", "click", function(){
  stopTimedLoop();
});

Syntax

stopTimedLoop(loop);

Parameters

NameTypeRequired?Description
loop Number The value returned by the `timedLoop()` function that you want stop. If not included, all running timed loops will stop.

Tips

  • Without a parameter, all running timed loops will be stopped.
  • If you want to a specific loop you need to save the value returned by timedLoop(), eg `var i = timedLoop(ms, callback);

Found a bug in the documentation? Let us know at documentation@code.org