Skip to content

Instantly share code, notes, and snippets.

@cdmz
Created September 16, 2015 12:28
Show Gist options
  • Select an option

  • Save cdmz/96b2b5f0628624279774 to your computer and use it in GitHub Desktop.

Select an option

Save cdmz/96b2b5f0628624279774 to your computer and use it in GitHub Desktop.
jquery ajax with abort previous xhr
var xhr_ = null ,xhr = null,timeout_seconds = 10*1000 ;
$("body").on('click',function(){
load_ajax();
});
function load_ajax(){
xhr_ = xhr;
xhr = $.ajax({
url: 'http://localhost/teste.php',
type: 'GET',
dataType: 'json',
timeout: timeout_seconds,
data: {
dados: 'value1'
},
beforeSend: function(){
if( xhr_ != null && typeof xhr_ == 'object' ) {
xhr_.abort();
}
},
success: function(response , status, xhr){
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment