2013-03-17 37 views
0

正如你看到我下面的代码运行异步的防止运行JavaScript ayschronous

 
} else { 
    liveInfo 
    .fadeOut(2000) 
    .html('در حال پخش : ' + current) 
    .fadeIn(1500) 
    .delay(7000); 
    if (next != ''){ 
     liveInfo 
     .fadeOut(2000) 
     .html('بعدی : ' + next) 
     .fadeIn(1500) 
     .delay(5000) 
    } 
} 

两个部分,我不能看到第一部分的结果。

哪,我看不到它的结果是:

 
    liveInfo 
    .fadeOut(2000) 
    .html('در حال پخش : ' + current) 
    .fadeIn(1500) 
    .delay(7000); 

现在我想避免运行if块异步与上述部分。

我能做到这样,但它迫使我有更多的相同代码:

 
} else { 
    if (next != ''){ 
     liveInfo 
     .fadeOut(2000) 
     .html('در حال پخش : ' + current) 
     .fadeIn(1500) 
     .delay(7000) 
     .fadeOut(2000) 
     .html('بعدی : ' + next) 
     .fadeIn(1500) 
     .delay(5000) 
    } else { 
     // this code is repeated! 
     liveInfo 
     .fadeOut(2000) 
     .html('در حال پخش : ' + current) 
     .fadeIn(1500) 
     .delay(7000) 
    } 
} 

是什么做的最好方法是什么?

回答

1

要进行同步的任务 - 像if条件或.html() - 等待在网上,许多动画功能为您提供可以使用的回调。而且,对于那些不这样做(如.delay()),可以在任务由.queue()包裹它们添加到相同的fx队列:

liveInfo 
.fadeOut(2000, function() { 
    liveInfo.html('در حال پخش : ' + current); 
}) 
.fadeIn(1500) 
.delay(7000) 
.queue(function (done) { 
    if (next != ''){ 
     liveInfo 
     .fadeOut(2000, function() { 
      liveInfo.html('بعدی : ' + next); 
     }) 
     .fadeIn(1500) 
     .delay(5000) 
    } 
    done(); 
}); 
+0

对于任何'动画'(包括'fadeIn/Out'),对于'html()'而言,回调参数将比排队一个额外函数更好。 – Bergi 2013-03-17 21:07:13

+0

@Bergi真实。但是'.delay()'不提供像'.fadeIn()'/'。fadeOut()'这样的回调,在这种情况下这将是必要的。 – 2013-03-17 21:09:50

+0

是的,那很伤心。我已经想知道它是否应该算作一个bug :-) – Bergi 2013-03-17 21:16:43

0
} else { 
    liveInfo 
    .fadeOut(2000) 
    .html('در حال پخش : ' + current) 
    .fadeIn(1500); 
    if (next != ''){ 
     liveInfo 
     .delay(7000) /* move the delay to the next animation */ 
     .fadeOut(2000) 
     .html('بعدی : ' + next) 
     .fadeIn(1500) 
     .delay(5000) 
    } 
} 

我认为只是将延迟移动到下一个动画应该为你想要的。

+0

但我想运行像脚本我的代码! – 2013-03-17 20:51:23