【发布时间】:2013-03-06 15:34:41
【问题描述】:
使用以下代码,Firebug 控制台中的所有内容都按顺序正常显示,条目按顺序(包括数据的 Ajax 发布),但所有 jQuery 效果/动画(fadeIn、animate、@987654324 @、slideDown、animate、fadeOut) 在调用我的 Ajax 函数后一次同时发生,而不是在执行实际步骤时发生。
为什么要这样做?我该如何解决?
$('#overlay').fadeIn(500, function () {
$('#overlaybox').animate({ 'top': '40px' }, 100);
});
$('#survey').slideUp(1500);
console.log('-after overlay show');
console.log('before ajaxPutSurveyItems()');
ajaxPutSurveyItems(save, after);
console.log('after ajaxPutSurveyItems()');
$('#survey').slideDown(1500);
$('#overlaybox').animate({ 'top': '-200px' }, 300, function () {
$('#overlay').fadeOut(2500);
});
console.log('-after overlay hide');
这里是 ajaxPutSurveyItems
function ajaxPutSurveyItems(answers, nextstep) {
console.log('ajaxPutSurveyItems()');
var p = {};
p.uniqueid = gUniqueID;
p.answers = answers;
p.pageid = gSurveyPages[gCurrentPageIndex].ID;
p.pageindex = gCurrentPageIndex.toString();
p.surveydefid = gSurveyID;
p.bypass = gIsBypass;
var j = JSON.stringify(p);
$.ajax({
async: false,
type: "POST",
url: "surveydata.asmx/putSurveyItems",
data: j,
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend: function(XHR) {
console.log(':beforeSend');
},
success: function (data, status) {
console.log(':success');
// code removed for brevity
},
error: function (XHR, errStatus, errThrown) {
console.log(':error');
// code removed for brevity
}
});
}
【问题讨论】:
-
你能把整个页面贴出来,包括HTML吗?
-
我建议阅读“回调”函数。
-
ajax 调用是同步还是异步?
-
代码很大,不宜发帖。基本上一个按钮单击事件会触发此代码,并且在 Firebug 控制台中看起来一切正常,但效果/动画都在调用我的 Ajax 数据保存例程之后执行。这是在 Ajax 调用中使用 beforeSend: 和 complete: 的问题的简化版本。