Skip to content

TimerAPI

Greg edited this page Oct 16, 2013 · 12 revisions

Timers are created on the server with cloak.createTimer. See ServerAPI.

Timers attempt to compensate for network latency when synchronizing with the client.

Parameters

The 3 parameters when creating a timer are:

  • name the name of the timer. It is used for timer.sync
  • millis the starting value of the timer
  • descending if the timer value decreases over time, rather than increasing (a countdown vs a stopwatch)

Example

// On the server
var timer = cloak.createTimer('myTimer');
timer.start();
// later
timer.sync(user);

// Client configuration
client.configure({
  ...
  timerEvents: {
    myTimer: function(millis) {
      console.log('myTimer value: ' + millis);
    }
  }
});

Methods

timer.sync(user)

If the user's client has a timer event handler with the same name as this timer, that event handler is called. Timer event handlers are declared in the timerEvents section of the ClientConfiguration. The argument to the event handler is the current milliseconds value of the timer. The timer.sync method attempts to compensate for network latency.

timer.start()

Start the timer. Timers are initially stopped.

timer.stop()

Stop the timer.

timer.getValue()

Get the current milliseconds value of the timer.

timer.reset(millis)

Stop the timer and set its value to millis. If millis is omitted, sets the timer to the last value it was reset with or the value it was created with.

Clone this wiki locally