# Yahoo! UI Library

### datasource 3.0.0

Yahoo! UI Library > datasource > datasource-polling.js (source view)
Search:
Filters Show Private Show Protected Show Deprecated

    /**
     * Extends DataSource with polling functionality.
     *
     * @module datasource
     * @submodule datasource-polling
     */

    /**
     * Adds polling to the DataSource Utility.
     * @class Pollable
     * @extends DataSource.Local
     */
    var LANG = Y.Lang,

        Pollable = function() {
            this._intervals = {};
        };

    Pollable.prototype = {

        /**
        * @property _intervals
        * @description Hash of polling interval IDs that have been enabled,
        * stored here to be able to clear all intervals.
        * @private
        */
        _intervals: null,

        /**
         * Sets up a polling mechanism to send requests at set intervals and forward
         * responses to given callback.
         *
         * @method setInterval
         * @param msec {Number} Length of interval in milliseconds.
         * @param request {Object} Request object.
         * @param callback {Object} An object literal with the following properties:
         *     <dl>
         *     <dt><code>success</code></dt>
         *     <dd>The function to call when the data is ready.</dd>
         *     <dt><code>failure</code></dt>
         *     <dd>The function to call upon a response failure condition.</dd>
         *     <dt><code>argument</code></dt>
         *     <dd>Arbitrary data that will be passed back to the success and failure handlers.</dd>
         *     </dl>
         * @return {Number} Interval ID.
         */
        setInterval: function(msec, request, callback) {
            var x = Y.later(msec, this, this.sendRequest, [request, callback], true);
            this._intervals[x.id] = x;
            return x.id;
        },

        /**
         * Disables polling mechanism associated with the given interval ID.
         *
         * @method clearInterval
         * @param id {Number} Interval ID.
         */
        clearInterval: function(id, key) {
            // In case of being called by clearAllIntervals()
            id = key || id;
            if(this._intervals[id]) {
                // Clear the interval
                this._intervals[id].cancel();
                // Clear from tracker
                delete this._intervals[id];
            }
        },

        /**
         * Clears all intervals.
         *
         * @method clearAllIntervals
         */
        clearAllIntervals: function() {
            Y.each(this._intervals, this.clearInterval, this);
        }
    };

    Y.augment(Y.DataSource.Local, Pollable);

Copyright © 2009 Yahoo! Inc. All rights reserved.