ajax reload,jQuery ajax Page Reload

Came across here pursuing a similar problem and decided to answer even though it's quite late for other people who might end up here with same problem.

I believe what you need is Ajax global events.

See API Documentation

Especially here;

Global Events

These events are triggered on the document, calling any handlers which

may be listening. You can listen for these events like so:

$(document).bind("ajaxSend", function(){

// You should use "**ajaxStop**" instead of "ajaxComplete" if there are more

// ongoing requests which are not completed yet

}).bind("ajaxStop", function(){

// call your reload function here

});

Now for your case, instead of binding "ajaxComplete" event if you use "ajaxStop" this will be triggered when all Ajax requests being processed are finished.

I copy-pasted your original code on fiddle and added the part I just recommended with some logs. jsfiddle.net/Tt3jk/7/ For testing purposes I called a similar SendData2() function from within your first function's success event to simulate an ugly async request scenario. If you test this code on a real environment(or place the SendData2 with your url that responds with your data type which was "text" what you should see on the console is this output.

(1- is console.log from SendData() and 2- is from SendData2()):

1-sending...

waiting for all requests to complete...

1-success:!

2-sending...

waiting for all requests to complete...

1-done:

2-success:!

2-done:

completed now!

You can in fact even see it even on fiddle(with errors on the requests) when your reload function is being called. If you use "ajaxComplete", reload function inside your jQuery .click() function is being called quite early. However if you use "ajaxStop" and call reload function when "ajaxStop" event is triggered, reload function will be called after all the requests are completed.

I don't know if fiddle disappears after a while so I will post the changes I made here as well without console logs:

$(".submit_button").click(function () {

popupMessage();

sendData(); //the ajax calls are all in here

// consider reloading somewhere else

});

$(document).bind("ajaxSend", function () {

console.log("waiting for all requests to complete...");

// ajaxStop (Global Event)

// This global event is triggered if there are no more Ajax requests being processed.

}).bind("ajaxStop", function () {

// maybe reload here?

location.reload();

});

function popupMessage() {

alert("Pop!");

}

function sendData() {

//a bunch of these:

$.ajax({

"dataType": "text",

"type": "POST",

"data": "temp",

"url": "your url here!",

"beforeSend": function (msg) {

console.log("1-sending...");

},

"success": function (msg) {

console.log("1-success!");

sendData2(); // again

},

"error": function (msg) {

console.log("1-error!");

}

}).done(function (msg) {

console.log("1-done!");

});

}

function sendData2() {

//a bunch of these:

$.ajax({

"dataType": "text",

"type": "POST",

"data": "temp",

"url": "your url here!",

"beforeSend": function (msg) {

console.log("2-sending...");

},

"success": function (msg) {

console.log("2-success!");

},

"error": function (msg) {

console.log("2-error!");

}

}).done(function (msg) {

console.log("2-done!");

});

}

PS. Not sure if it's a good practice or not to make another request from within a request, probably not. But I put it there to show how "ajaxStop" event is delayed to be triggered until all ongoing requests are done(or completed with error at least)...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值