Timed Loop

The for loop is a really useful structure for repeating something many times quickly, or iterating over a list of things, but it doesn't do much good if you want to repeat something more slowly. For cases when you want to control how quickly the repetition happens, you'll want a Timed Loop. timedLoop

The Structure of a Timed Loop

A Timed Loop is actually composed of two blocks:

  • The timedLoop() block, which takes two inputs:

    • The ms input determines how many milliseconds to wait before repeating the loop. The default value is 1000 milliseconds, or 1 second.
    • The callback input is the function that will be run each time the loop repeats.
  • The stopTimedLoop() block which stops all timed loops that are running.

How it Works

timedLoop diagram

  1. This whole block is the Timed Loop
  2. Number of milliseconds to wait after each repetition of the loop. 1000 milliseconds = 1 second.
  3. The green section is our callback function - the code that we run each time the loop repeats.
  4. The stopTimedLoop() block tells all running Timed Loops to stop running. Note that when stopTimedLoop() is called, the timed loop won't necessarily quit immediately, it just won't repeat next time.

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