【问题标题】:jQuery effects/animation is delayed in executionjQuery 效果/动画执行延迟
【发布时间】:2013-03-06 15:34:41
【问题描述】:

使用以下代码,Firebug 控制台中的所有内容都按顺序正常显示,条目按顺序(包括数据的 Ajax 发布),但所有 jQuery 效果/动画(fadeInanimate、@987654324 @、slideDownanimatefadeOut) 在调用我的 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: 的问题的简化版本。

标签: jquery animation effects


【解决方案1】:

如前所述,您不会等到一个动画完成后才开始另一个动画。您刚刚将它们设置为同时启动。虽然您已经使用了一些回调,但我相信您了解它们的工作原理并且只需要添加更多。

所以需要callbacks

 $('#overlay').fadeIn(500, function () { 
   $('#overlaybox').animate({ 'top': '40px' }, 100);
 });
 $('#survey').slideUp(1500, function(){

      console.log('-after overlay show');
      console.log('before ajaxPutSurveyItems()');

      ajaxPutSurveyItems(save, after);

      console.log('after ajaxPutSurveyItems()');

      $('#survey').slideDown(1500, function(){

            $('#overlaybox').animate({ 'top': '-200px' }, 300, function () {
              $('#overlay').fadeOut(2500); 
            });     
      });

      console.log('-after overlay hide');
 });

【讨论】:

  • 我在实际的 ajax 调用的 beforeSend: 中具有显示效果,而在 complete: 中具有隐藏效果,但发生了相同的效果。基本上按钮点击发生然后浏览器似乎被挂起,直到所有效果触发时 ajax 调用(当前调用 SQL sproc 延迟 5 秒)结束。
  • 谢谢!这很好用!在 slideDown 效果中没有拼写错误的“功能”。
猜你喜欢
  • 2010-09-20
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多