Return to Snippet

Revision: 40321
at January 30, 2011 06:14 by FatFolderDesigner


Initial Code
function ajax_get(){
	if(getting!=''){getting.abort();}
	$('#loading_animation').fadeIn();
	filter = $.get('/path/to/ajax.file', function(data) {
		$('#ajax_container').html(data);
		$('#loading_animation').fadeOut();
	});
}

Initial URL
http://fatfolderdesign.com/181/code/prevent-stale-ajax-data-in-jquery

Initial Description
When running live filters on large, long scripts you can get old, stale results replacing your new, fresh ones. After all, a search for everything containing 'S" in a large database of city names can take a lot longer than "Seattle", so your script gets the results for "Seattle", displays then, then it gets the results for "S" and displays them, replacing the "Seattle" search you really wanted.

This script aborts the previous call before making the new one, effectively preventing that from happening. There are 2 caveats though.

1. The server may continue to process the request (depending on setup).
2. The error console logs it as an error, but not a fatal one and no alerts are thrown so the normal end user wont notice.

I have some more information available at the URL.

Initial Title
Prevent Stale Ajax Data with jQuery

Initial Tags
ajax, jquery

Initial Language
jQuery