Reputation: 1323
I have a function who call a getJSON. But the getJSON call is often not finish when the function is call again.
So I want to stop the previous getJSON still running before send the new one.
I try to save my getJSON call in a global variable and call abort() in the beginning of my function, but I got an error
TypeError: vr is undefined
Here is my function
var vr;
function getVol() {
vr.abort();
var data = {
form : fields
};
vr = $.getJSON('getVol.php', data).done(function(r) {
$.each( r, function( key, item ) {
$.each( item, function( key, value ) {
if(parseInt(value) > maxValue) maxValue = parseInt(value);
});
});
}).fail(function() {
console.log( "error" );
});
}
Thanks for help
Upvotes: 3
Views: 3785
Reputation: 780843
You're getting an error because when you call the function the first time, vr
is empty. You need to check that it's set first.
function getVol() {
if (vr) {
vr.abort();
}
// rest of function
}
Upvotes: 6