App-I18N

 view release on metacpan or  search on metacpan

share/static/jquery.ev.js  view on Meta::CPAN

 *
 */
(function($){

  $.ev = {

    handlers : {},
    running  : false,
    xhr      : null,
    verbose  : true,
    timeout  : null,

    /* Method: run
     *
     * Respond to an array of messages using the object in this.handlers
     *
     */
    run: function(messages) {
      var i, m, h; // index, event, handler
      for (i = 0; i < messages.length; i++) {
        m = messages[i];
        if (!m) continue;
        h = this.handlers[m.type];
        if (!h) h = this.handlers['*'];
        if ( h) h(m);
      }
    },

    /* Method: stop
     *
     * Stop the loop
     *
     */
    stop: function() {
      if (this.xhr) {
        this.xhr.abort();
        this.xhr = null;
      }
      this.running = false;
    },

    /*
     * Method: loop
     *
     * Long poll on a URL
     *
     * Arguments:
     *
     *   url
     *   handler
     *
     */
    loop: function(url, handlers) {
      var self = this;
      if (handlers) {
        if (typeof handlers == "object") {
			this.handlers = handlers;
        } else if (typeof handlers == "function") {
			this.run = handlers;
        } else {
			throw("handlers must be an object or function");
        }
      }
      this.running = true;
      this.xhr = $.ajax({
        type     : 'GET',
        dataType : 'json',
        url      : url,
        timeout  : self.timeout,
		cache    : false,
        success  : function(messages, status) {
            self.run(messages);
        },
        complete : function(xhr, status) {
			var delay;
			if (status == 'success') {
				delay = 100;
			} else {
				delay = 5000;
			}
			// "recursively" loop
			window.setTimeout(function(){ 
				if (self.running) 
					self.loop(url);
			}, delay);
        }
      });
    }

  };

})(jQuery);



( run in 2.688 seconds using v1.01-cache-2.11-cpan-f56aa216473 )