在 ajax 回调函数 javascript 中执行乱序
execution out of order inside ajax callback function javascript
我有一个后续的 ajax 操作,旨在 (1) show spinner gif
在发送 ajax
请求之前,以及在请求完成之后,(2) hide
gif 和 3 display
适当的警报消息。
终于 (4) reload
页面了。
代码如下:
$.ajax({
url: rUrl,
data: {
id: rID,
requisitionStatus: rStatus,
comment: rComment
},
type: "POST",
cache: false,
beforeSend: function() {
$("#requisitionStatusDialog").dialog('close');
$('#ajax_loader_my').show();
},
success: function(data, resp) {
var json = data;
var obj = JSON && JSON.parse(json) || $.parseJSON(json);
if (obj.status == "success") {
alert('Success! ' + obj.message);
location.reload();
} else if (obj.status == "error") {
alert('Error!' + obj.message);
}
},
error: function(data, resp) {
$("#updateDialog").dialog('close');
console.log(resp);
},
complete: function() {
$('#ajax_loader_my').hide();
}
});
但在这种情况下,alert
先弹出,而spinner gif
仍然显示,reloads
点击OK
后的页面。
我什至尝试 hiding
success
回调本身中的 gif 而不是使用 complete
:
success: function(data, resp) {
var json = data;
var obj = JSON && JSON.parse(json) || $.parseJSON(json);
if (obj.status == "success") {
$('#ajax_loader_my').hide();
alert('Success! ' + obj.message);
location.reload();
} else if (obj.status == "error") {
alert('Error!' + obj.message);
}
},
两者给出相同的结果。
我宁愿建议使用空 div 或带有 Id 的跨度。
比在那个div的html显示成功。
例如:
$('#ajax_loader_my').hide();
setTimeout(function () {
$('#successDiv').html('Success! ' + obj.message);
location.reload();
}, 2000);
在隐藏微调器之前弹出警报的原因是成功代码在隐藏微调器的完成之前运行。它重新加载的原因是因为在您发送警报后 location.reload();
检查 $('#ajax_loader_my').hide();实际上隐藏了微调器。 html 中属于或包含微调器的元素必须将其 ID 设置为 ajax_loader_my。
如果您打开 Chrome 或 Firefox Dev 工具,您应该能够发送 $('#ajax_loader_my').hide() 并查看会发生什么。
以这种方式重写代码,这会将您的警报和位置相关代码放入事件队列中,运行 空闲时。
if(obj.status=="success") {
$('#ajax_loader_my').hide();
setTimeout(function(){
alert('Success! '+obj.message);
location.reload();
},0);
}
您好,您应该尝试使用 promises 这里是文档 Jquery promises,我是即时制作的,它可能有一些错误,但这只是一个例子:
$( function() {
function AjaxCall(rID,rStatus,rComment){
return $.ajax({
url: 'request.php',
data: {
id: rID,
requisitionStatus: rStatus,
comment: rComment
},
type: "POST",
cache: false,
beforeSend: function() {
$("#requisitionStatusDialog").dialog('close');
$('#ajax_loader_my').show();
}
})
}
$( "#requisitionStatusDialog" ).dialog();
$("#yourbuttonInputId").on('click',function(event) {
AjaxCall().done(function(data,response){
var obj = JSON.parse(data);
if (obj.status == "success") {
alert('whe are on done!');
}
}).fail(function(data,response){
$("#updateDialog").dialog(' close');
}).always(function(data){
if(confirm('You have finished the request. Click OK if you wish to continue ,click Cancel to reload the page.'))
{
$('#ajax_loader_my').hide();
$("#requisitionStatusDialog").dialog('open');
}else{
location.reload();
}
});
});
} );
编辑:检查这个jsfiddle它会指导你完善你的代码
希望对您有所帮助
我有一个后续的 ajax 操作,旨在 (1) show spinner gif
在发送 ajax
请求之前,以及在请求完成之后,(2) hide
gif 和 3 display
适当的警报消息。
终于 (4) reload
页面了。
代码如下:
$.ajax({
url: rUrl,
data: {
id: rID,
requisitionStatus: rStatus,
comment: rComment
},
type: "POST",
cache: false,
beforeSend: function() {
$("#requisitionStatusDialog").dialog('close');
$('#ajax_loader_my').show();
},
success: function(data, resp) {
var json = data;
var obj = JSON && JSON.parse(json) || $.parseJSON(json);
if (obj.status == "success") {
alert('Success! ' + obj.message);
location.reload();
} else if (obj.status == "error") {
alert('Error!' + obj.message);
}
},
error: function(data, resp) {
$("#updateDialog").dialog('close');
console.log(resp);
},
complete: function() {
$('#ajax_loader_my').hide();
}
});
但在这种情况下,alert
先弹出,而spinner gif
仍然显示,reloads
点击OK
后的页面。
我什至尝试 hiding
success
回调本身中的 gif 而不是使用 complete
:
success: function(data, resp) {
var json = data;
var obj = JSON && JSON.parse(json) || $.parseJSON(json);
if (obj.status == "success") {
$('#ajax_loader_my').hide();
alert('Success! ' + obj.message);
location.reload();
} else if (obj.status == "error") {
alert('Error!' + obj.message);
}
},
两者给出相同的结果。
我宁愿建议使用空 div 或带有 Id 的跨度。 比在那个div的html显示成功。 例如:
$('#ajax_loader_my').hide();
setTimeout(function () {
$('#successDiv').html('Success! ' + obj.message);
location.reload();
}, 2000);
在隐藏微调器之前弹出警报的原因是成功代码在隐藏微调器的完成之前运行。它重新加载的原因是因为在您发送警报后 location.reload();
检查 $('#ajax_loader_my').hide();实际上隐藏了微调器。 html 中属于或包含微调器的元素必须将其 ID 设置为 ajax_loader_my。
如果您打开 Chrome 或 Firefox Dev 工具,您应该能够发送 $('#ajax_loader_my').hide() 并查看会发生什么。
以这种方式重写代码,这会将您的警报和位置相关代码放入事件队列中,运行 空闲时。
if(obj.status=="success") {
$('#ajax_loader_my').hide();
setTimeout(function(){
alert('Success! '+obj.message);
location.reload();
},0);
}
您好,您应该尝试使用 promises 这里是文档 Jquery promises,我是即时制作的,它可能有一些错误,但这只是一个例子:
$( function() {
function AjaxCall(rID,rStatus,rComment){
return $.ajax({
url: 'request.php',
data: {
id: rID,
requisitionStatus: rStatus,
comment: rComment
},
type: "POST",
cache: false,
beforeSend: function() {
$("#requisitionStatusDialog").dialog('close');
$('#ajax_loader_my').show();
}
})
}
$( "#requisitionStatusDialog" ).dialog();
$("#yourbuttonInputId").on('click',function(event) {
AjaxCall().done(function(data,response){
var obj = JSON.parse(data);
if (obj.status == "success") {
alert('whe are on done!');
}
}).fail(function(data,response){
$("#updateDialog").dialog(' close');
}).always(function(data){
if(confirm('You have finished the request. Click OK if you wish to continue ,click Cancel to reload the page.'))
{
$('#ajax_loader_my').hide();
$("#requisitionStatusDialog").dialog('open');
}else{
location.reload();
}
});
});
} );
编辑:检查这个jsfiddle它会指导你完善你的代码
希望对您有所帮助