|
DelayedObserver
jquery.delayedobserver.js
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. ExampleHere'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
| |||||
► Sign in to add a comment
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!
The parameters are backwards... should be callback then delay...
@andrew.flanagan:
Sorry I forgot to update this doc for a while, it's fixed.
Very simple example:
$("#email").delayedObserver(function () { console.log("#email:" + $(this).val()); });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?