Source

jquery_counter /

Filename Size Date modified Message
demo
27 B
1.0 KB
1.9 KB
828 B
818 B

jQuery counter plugin

Overview

This plugin gives you a nice and clean way to create callbacks that would be called after several conditions are met. It's primary use is to wrap functions that require several ajax requests to be fullfilled (as we don't know in what order they will arrive).

Requirements

The plugin should work from jQuery 1.2 up.

Usage

  1. Create the counter object by calling $.counter(fn[, scope[, args]]) on the function you want to act as a callback. Optionally you can pass execution scope and argument list.
  2. Call the created object's require('name') method for every condition specifying a string that would act as an id for this condition.
  3. Use a call to counter's signal('name'[, args]) to signal that the condition was met. You can optionally modify the argument list to the function.

Example

This code will load both header and footer elements and will call the main_callback() after last of them is loaded.

function main_callback() {
        // Do something when both header and footer are loaded
}

main_callback = $.counter(main_callback);

main_callback.require('header');
$('#header').load('http://example.com/header.html', function () {
        // Do something with the header
        main_callback.signal('header');
});

main_callback.require('footer');
$('#footer').load('http://example.com/footer.html', function () {
        // Do something with the footer
        main_callback.signal('footer');
});

Howto run demo

  1. Install paste:

    pip install Paste
    
  2. Download jquery and put it in the main directory.

  3. Run the demo server:

    cd demo
    python demo.py
    
  4. Point your browser to http://127.0.0.1:8642