Delayed AJAX update

Doing a lot of small AJAX based updates is rather expensive. To solve this my idea was that an AJAX update should be made when the user is finished doing changes (that way you don't make an AJAX update for every change, but only one when the user is finished doing changes).

Here is some pseudo code that delays the AJAX update 10 seconds every time an user edits something:

var Timeouts = {};

function ajaxUpdate() {
   alert('We AJAX update in here!');
}

function updateItem(item_id) {
   ...
   var timeout = Timeouts[item_id];
   if(timeout)
      clearTimeout(timeout);
   Timeouts[item_id] = setTimeout(ajaxUpdate, 10000);
}
AJAX and comet · Code · Code improvement · JavaScript · Tips 11. Sep 2007
© Amir Salihefendic. Powered by Skeletonz.