JugglingBob
JugglingBob

Reputation: 275

How can I disable a getJSON request if it hasn't completed in one second and move onto the next one?

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

Answers (1)

Sysix
Sysix

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

Related Questions