PencilCoder

Timers (forever!)

Goals:

  • Use the Pencil Code forever function to set up Timing events
  • Transition from queue-based to frame-based animation

Coding Snippets:

  • coding snippets
  • forever sets up a recurring timer that by default executes a callback 30 times per second. Set the timer to fire more or less frequently by specifying the optional first argument.
  • forever returns a unique id for each timer
  • Remove a timer from within its callback by calling stop(). Outside of its own callback, remove a timer by passing its ID as the argument to stop.

Math and Computer Concepts:

  • forever is a Pencil Code function that facilitates executing a callback repeatedly at fixed intervals. It is an alternative to setting up a timing event using the built-in JavaScript function setInterval.
  • Remove a timer created with forever using the stop method. From within a timer's callback, stop() (with no arguments) references that timer. Outside of the callback, stop() removes all timers. stop(x) removes a timer with an ID referenced by x.
  • When working with forever loops, it is often advantageous to work with frame-based animation. Call speed Infinity at the beginning of your script, and no longer worry about animation queues! The tradeoff is that you have to code the animation logic yourself.

Activities:

Save each of the following in a folder called Forever.