jquery.poll – a periodic polling plugin for jquery.

I needed to poll the server for another project we’re working on (to be announced soon). I started off using the PeriodicalUpdater from enfranchised mind, but I ran into some limitations that were problematic enough that I implemented a new version from scratch.

The problem that made me re-implement was that I was polling for new posts, and if the user made a new post I wanted the wait between poll attempts to go back to the minimum.

The main improvements of this version over the enfranchised mind version are:

  • Multiple active polls
  • Ability to modify the ajax settings after the poll has started
  • Can make a one-time change to the current polling interval.

You can download or contribute from the project page.

Usage:

//All poll options are optional.
var pollopts = {
  //A random name will be assigned if you don't assign one
  name: 'name-of-poll',
  //minimum wait between calls in msec
  min_wait: 6000, // default: 1000
  //maximum wait between calls in msec
  max_wait: 12000, // default: 30000
  // amount to multiply the wait by if the data is unchanged.
  wait_multiplier: 4, // default: 2
  // log poll events to console.log
  doLog: false, // default true
  // A function that overrides the default calculation
  // for how to change the wait if nothing has changed.
  // Should return an integer
  adjustWait: function(xhr, textStatus, current_wait) { ... }, .
};
// simplest way to start polling.
// ajaxopts are defined as per
// http://docs.jquery.com/Ajax/jQuery.ajax#toptions
var mypoll = $.poll(ajaxopts);
// start polling with custom poll options
var mypoll = $.poll(ajaxopts,pollopts);
// get a poll object, but don't start polling
var mypoll = $.poll(ajaxopts,pollopts,true);
// can also do $.poll(ajaxopts,null,true);
// stop the poll
mypoll.stop = true;
// make a one-time change to the poll wait interval
mypoll.setWait(500);
// change the ajax options for the live poll
mypoll.setAjaxOptions(ajaxopts);
You can leave a response, or trackback from your own site.

4 Responses to “jquery.poll – a periodic polling plugin for jquery.”

  1. [...] the project page or the canhas post for more details. This entry was posted on Sunday, January 10th, 2010 at 22:45. You can follow [...]

  2. That’s an interesting set of requirements, and the version hanging off my blog should be able to support it with a few new API calls—or am I missing something?

  3. mindlace says:

    I couldn’t figure out how to adapt your version to give me control over the poller after it was created. Specifically, your code doesn’t track the return result from the setTimeout() call (so cancelTimeout isn’t possible). Anyway, I worked on adapting it for a few hours before I gave up and wrote a different version.

    Gods knows it turned out to be a pain in the butt because of the way self is bound to the window object for the callback, but I figured that out eventually.

    Also, the fact that the timer re-set when the server had an error seemed like something I didn’t want – if my server is having problems the last thing I want is for all the clients to be polling me every (minimum timeout) interval.

    It was really useful in development- thanks for your work!

  4. ecco says:

    don’t mind me, testing blog artifacts.

Leave a Reply