Chris
Chris

Reputation: 111

How do I stop a jQuery long-polling request?

I'm using...

   $.getJSON(url + "&callback=?", function (b) {
                .......
   });

for a long-polling request. Sometimes it is necessary that I stop the current request being made. Is this possible?

Upvotes: 2

Views: 1862

Answers (2)

derek
derek

Reputation: 4886

Not sure if you're aborting because it's taking too long, but if so you can change your $.getJSON call to $.ajax and set a timeout:

$.ajax({
  url: url,
  dataType: 'json',
  data: "param1=" + params,
  timeout: 7000,
  success: resultsHandler
})

Upvotes: 0

user113716
user113716

Reputation: 322492

I think this works, but have never tried it for myself...

var theRequest = $.getJSON(url + "&callback=?", function (b) {
            .......
});

theRequest.abort();  // aborts the xmlhttprequest made

$.getJSON() should return the XMLHTTPRequest object, upon which you call the abort() method.

Upvotes: 4

Related Questions