src code

class Ajax.PeriodicalUpdater

Description

Periodically performs an Ajax request and updates a container's contents based on the response text.

Ajax.PeriodicalUpdater behaves like Ajax.Updater, but performs the update at a prescribed interval, rather than only once. (Note that it is not a subclass of Ajax.Updater; it's a wrapper around it.)

This class addresses the common need of periodical update, as required by all sorts of "polling" mechanisms (e.g., an online chatroom or an online mail client).

The basic idea is to run a regular Ajax.Updater at regular intervals, keeping track of the response text so it can (optionally) react to receiving the exact same response consecutively.

Additional options

Ajax.PeriodicalUpdater features all the common options and callbacks described in the Ajaxplus those added by Ajax.Updater.

It also provides two new options:

  • frequency (Number; default is 2): How long, in seconds, to wait between the end of one request and the beginning of the next.
  • decay (Number; default is 1): The rate at which the frequency grows when the response received is exactly the same as the previous. The default of 1 means frequency will never grow; override the default if a stale response implies it's worthwhile to poll less often. If decay is set to 2, for instance, frequency will double (2 seconds, 4 seconds, 8 seconds...) each consecutive time the result is the same; when the result is different once again, frequency will revert to its original value.
Disabling and re-enabling a Ajax.PeriodicalUpdater

You can hit the brakes on a running Ajax.PeriodicalUpdater by calling Ajax.PeriodicalUpdater#stop. If you wish to re-enable it later, call Ajax.PeriodicalUpdater#start.

Instance methods