开发者

How do I stop a jQuery long-polling request?

开发者 https://www.devze.com 2022-12-30 02:14 出处:网络
I\'m using... $.getJSON(url + \"&callb开发者_运维百科ack=?\", function (b) { ....... }); for a long-polling request.Sometimes it is necessary that I stop the current request being made.Is this

I'm using...

   $.getJSON(url + "&callb开发者_运维百科ack=?", function (b) {
                .......
   });

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


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.


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
})
0

精彩评论

暂无评论...
验证码 换一张
取 消