开发者

How do I stop a jQuery long-polling request?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜