As the error says, numbers don't have an abort
method. Your load_info
is a number, the handle returned by setTimeout
. It's not an XHR or jqXHR object.
If you want to cancel the timer (prevent the ajax request from being started), use:
clearTimeout(load_info);
If you want to cancel the in-progress ajax call once it is started, you'll have to get the XHR object from ajax
and then call abort
on it, like this:
var loading_xhr = null;
$('body').on('click', '#moreinfo', function() {
setTimeout(function(){
loading_xhr = $.ajax({
url:'file.php',
method:"GET",
success:function(s){ alert(s); },
error:function(e){ alert(e); },
complete:function(){
// Be sure to clear it again here
loading_xhr = null;
};
})
},2000);
});
$('body').on('click', '#cancel', function() {
if (loading_xhr) {
loading_xhr.abort();
loading_xhr = null;
}
});