最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - in jquery how to call a function after an ajax request has been sent? - Stack Overflow

programmeradmin0浏览0评论

How can I call my function: myFunc1 in jquery after the browser has pletely sent an ajax request?

I want to run a callback function after a jquery ajax has been sent. I don't want to do this on plete or success because I don't want to wait for the request to plete. I want to fire it right after the ajax request has been sent from the browser.

This is use case for which i need this:

I want run a function after making an ajax request. My original code was this:

  $.ajax({url: 'x.html'});
  myFunc1();

In the case when `myFunc1() changes the browsers location, the browser would cancel the pending ajax request.

My current solution is to do this:

  $.ajax({url: 'x.html', plete: myFunc1});

This makes sure that myFunc1 is not called till the ajax is plete. Although this solution works, it is inefficient because myFunc1 wont run till the request is plete. Since I don't care what the ajax request returns, whether it fails or succeeds, I dont need to wait for it to plete to run the myFunc1 function.

How can I call my function: myFunc1 in jquery after the browser has pletely sent an ajax request?

I want to run a callback function after a jquery ajax has been sent. I don't want to do this on plete or success because I don't want to wait for the request to plete. I want to fire it right after the ajax request has been sent from the browser.

This is use case for which i need this:

I want run a function after making an ajax request. My original code was this:

  $.ajax({url: 'x.html'});
  myFunc1();

In the case when `myFunc1() changes the browsers location, the browser would cancel the pending ajax request.

My current solution is to do this:

  $.ajax({url: 'x.html', plete: myFunc1});

This makes sure that myFunc1 is not called till the ajax is plete. Although this solution works, it is inefficient because myFunc1 wont run till the request is plete. Since I don't care what the ajax request returns, whether it fails or succeeds, I dont need to wait for it to plete to run the myFunc1 function.

Share Improve this question edited Sep 5, 2013 at 18:03 user566245 asked Sep 4, 2013 at 14:03 user566245user566245 4,2471 gold badge34 silver badges36 bronze badges 1
  • 1 If you're not interested in a callback then just fire myFunc1 at the same time as your ajax call. – Matt Ryan Commented Sep 7, 2013 at 3:14
Add a ment  | 

4 Answers 4

Reset to default 0

If you look at the specification:

http://www.w3/TR/XMLHttpRequest/

You'll see 2 means the headers have been returned already.

const unsigned short **HEADERS_RECEIVED** = 2;

This is essentially the same as jqXHR success. Your browser may vary =)

No seriously.. they will vary, another reason jquery can't support them all and chooses to abstract them:

http://msdn.microsoft./library/ie/ms534361.aspx

https://developer.mozilla/en-US/docs/Web/API/document.readyState

(couldn't find an authoritative source for chrome)

Actually sounds like you want the function to fire upon the start of the AJAX request.

$("#btn-ajax").ajaxStart(myFunc1);

function myFunc1() {
   $("#message").html("I don't really care if the ajax call succeeds.");
}

This is what i did to run my callback function sufficiently after the AJAX call had been made so the browser doesn't cancel it and without having to wait for the server to plete the resquest:

  var cb = function(){
    window.document.location.href="http://google.";
  }

  $.ajax({
    url: 'x.html',
    plete: cb,
    timeout: 100
  });

Here the timeout makes sure that if my server doesn't repond in 100ms, the callback is executed. 100ms also is sufficient time for the browser to send the request.

I experimented with status codes and finally ended up not using them. I found them to unreliable cross browser and for JSONP requests missing.

I think you want to use the beforeSend option.

see http://api.jquery./jQuery.ajax/

发布评论

评论列表(0)

  1. 暂无评论