2011-09-25 98 views
0

我有2个功能,我想一次又一次地重复:不会jQuery的切换不会再次触发

function rotation() { 
     plrotation(); 
     function plrotation() { 
      alert('pl'); 
      $('#pl').toggle(300).delay(10000).toggle(400).delay(2000, function(){errotation()}); 
      alert('pl end'); 
     } 
     function errotation() { 
      alert('er'); 
      $('#er').toggle(300).delay(10000).toggle(400).delay(2000, function(){plrotation()}); 
      alert('er end'); 
     } 
    } 

但第一次后,我得到的“PL”和“PL结束”的警报,但切换发生,并且错误也没有开始......任何我做错的想法是错误的? (show element 1 => wait => hide element1 => wait => show element 2 => wait => hide element 2 => wait => restart)

回答

2

您最后致电jQuery.delay()是不正确的。延迟只会延迟队列,它不会像其他动画函数那样需要完成处理程序。它不能用来取代setTimeout()

因此使用setTimeout()而不是delay()

function rotate(el) { 
    var $el = $(el); 
    $el.toggle(300).delay(10000).toggle(400, function() { 
     var next = $el.is('#pl') ? '#er' : '#pl'; 
     setTimeout(function() { rotate(next); }, 2000); 
    }); 
} 

$(function() { rotate('#pl')}); 
+0

OK了... .. MHM现在我觉得哑巴...... :) –

+0

哦,非常感谢你! :) –