Reputation: 275
setInterval(function() {
$.getJSON('https://example.com', function(data) {
console.log(data)
});
}, 1000);
Currently every second, my script outputs data from a JSON file in the console log.
However if it does not do this in time, the requests build up and a huge backlog appears! How can I cancel the current getJSON request and move onto the next one if it has not completed in the time frame?
Upvotes: 0
Views: 93
Reputation: 1796
You can get the xhr
object and kill it with abort
evertime.
var xhr = $.getJSON('https://example.com', function(data) {
console.log(data)
});
//kill the request after 500ms
setTimeout(xhr.abort, 500);
Upvotes: 1