diff --git a/src/ajax/periodical_updater.js b/src/ajax/periodical_updater.js index 83ea294..03fa516 100644 --- a/src/ajax/periodical_updater.js +++ b/src/ajax/periodical_updater.js @@ -1,4 +1,62 @@ +/** section: Ajax + * class Ajax.PeriodicalUpdater + * + * 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 [[Ajax section]] — _plus_ 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 `PeriodicalUpdater`

+ * + * You can hit the brakes on a running `PeriodicalUpdater` by calling + * [[Ajax.PeriodicalUpdater#stop]]. If you wish to re-enable it later, call + * [[Ajax.PeriodicalUpdater#start]]. + * +**/ + Ajax.PeriodicalUpdater = Class.create(Ajax.Base, { + /** + * new Ajax.PeriodicalUpdater(container, url[, options]) + * - container (String | Element): The DOM element whose contents to update + * as a result of the Ajax request. Can be a DOM node or a string that + * identifies a node's ID. + * - url (String): The URL to fetch. When the _same-origin_ policy is in + * effect (as it is in most cases), `url` **must** be a relative URL or an + * absolute URL that starts with a slash (i.e., it must not begin with + * `http`). + * - options (Object): Configuration for the request. See the + * [[Ajax section]] for more information. + * + * Creates a new `Ajax.PeriodicalUpdater`. + **/ initialize: function($super, container, url, options) { $super(options); this.onComplete = this.options.onComplete; diff --git a/src/ajax/updater.js b/src/ajax/updater.js index e1efa98..036dbe8 100644 --- a/src/ajax/updater.js +++ b/src/ajax/updater.js @@ -17,16 +17,100 @@ * parameters); it will then replace the contents of the element with the ID * of `items` with whatever response it receives. * + *

Callbacks

* + * `Ajax.Updater` supports all the callbacks listed in the [[Ajax section]]. + * Note that the `onComplete` callback will be invoked **after** the element + * is updated. + * + *

Additional options

+ * + * `Ajax.Updater` has some options of its own apart from the common options + * described in the [[Ajax section]]: + * + * * `evalScripts` ([[Boolean]]; defaults to `false`): Whether `