suggestion: delayed related AJAX requests
Here's a suggestion for a feature that I think would be useful to AJAX
developers. Sometimes, we need an AJAX feature that could allow a user
to cause a large number of consequent AJAX requests, for example in
auto-complete for searches (like Google Suggest) or filter forms that
submit whenever something is changed in them. In a case like this,
especially when the user has a slow internet connection, we have no
way of knowing if the AJAX responses will arrive in the same order as
the requests were sent. A solution to this is to wait for some time to
make sure that the user has stopped manipulating the controls that
cause AJAX requests before sending a request. At the moment, I do it
like this:
function submit_filter() {
$('#item_list').html('loading...');
$.post('some url',$('#filter').serialize(),function(data) {
$('#item_list').html(data);
});
return false;
}
$('.ajax_control').change(function() {
clearTimeout(ajaxTimeout);
ajaxTimeout = setTimeout(submit_filter,2000);
});
Basically, whenever a control is changed a timeout for ajax request is
reset to 2 seconds. A request is sent only when a user changes
something an hasn't made new changes last 2 seconds.
Since this is a frequent use case, it would be great if there would be
a $.post_delayed() function which would have additional 2 parameters:
an id, which would be used to identify related controls, and a time
interval to wait before submitting the request.