My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
DelayedObserver  
Updated Aug 6, 2010 by hainea...@gmail.com

jquery.delayedobserver.js

Author Copyright (c) 2007-2008 Maxime Haineault (haineault.com)
License MIT License

A delayed observer, useful for handling user inputs to avoid calling the callback function too often.

Autocomplete fields is a common situation where this mechanism is useful. You must post pone the AJAX call until the user stops typing to prevent fast typers to fire too much requests to the server and most likely hang the browser.

Example

Here's an example that will ensure a minimum of 500 miliseconds delay between each callbacks:

<input type="text" id="autocomplete" />
$('#autocomplete').delayedObserver(function() {
    alert('Sending "'+ $(this).val() +'" to the server.');
    // perform AJAX call ...
}, 0.5);

By default it listen for keyup events, but you can change that like this:

$('#autocomplete').delayedObserver(function() {
    alert('Sending "'+ $(this).val() +'" to the server.');
    // perform AJAX call ...
}, 0.5, {
    event: 'keydown'
});

Sometimes it might be useful to change the condition of the comparison, for example, to make it case insensitive. You can achieve this using the condition option;

$('#autocomplete').delayedObserver(function() {
    // AJAX call
}, 0.5, {
    condition: function() { 
        return ($(this).data('oldval').toLowerCase() == $(this).val().toLowerCase());
    }
});

Tested browsers

  • Microsoft Internet Explorer 7 - Windows XP/MC
  • Mozilla Firefox 2 - Windows XP/MC / Ubuntu
  • Gnome Epiphany - Ubuntu
Comment by sathia.m...@gmail.com, Jan 21, 2009

can you please provide an example more clear? i can't get it working it returns $obj.data("callback").apply is not a function

where am I wrong?

thanks!

Comment by andrew.f...@gmail.com, Apr 15, 2009

The parameters are backwards... should be callback then delay...

Comment by project member hainea...@gmail.com, Jun 18, 2009

@andrew.flanagan:

Sorry I forgot to update this doc for a while, it's fixed.

Comment by rake36, Aug 6, 2010

Very simple example:

$("#email").delayedObserver(function () {
  console.log("#email:" + $(this).val());
});
Comment by pasca...@gmail.com, May 27, 2011

hello I am using jQuery delayed observer - 0.3 , but I would use it on the same page for more items, it works only with the last one. Can anything be done?

Thanks, Pasquale


Sign in to add a comment
Powered by Google Project Hosting