2013-02-24 154 views
0

通過元素此腳本無限循環,並應用類:僅在其他功能完成後執行功能嗎?

http://jsfiddle.net/6HDAW/

function across() { 
var $active = $('.div .current'); 
var $next = $active.next();  
$next.addClass('current'); 
$active.removeClass('current'); 
if($next.length == 0) { $(".div .bed:first-child").addClass('current'); } 
} 

function down() { 
    var $active = $('#slideshow .active'); 
    var $next = $active.next();  
    $next.addClass('active'); 
    $active.removeClass('active'); 
    if($next.length == 0) { $("#slideshow .div:first-child").addClass('active'); } 
} 

$(function() { 
    setInterval('across()', 500); 
    setInterval('down()', 500); 
}); 

有移動「跨越」的和功能的移動「下來」的.divs的功能。

我需要做些什麼改變才能讓函數'down'只在函數'across'完成時執行?

我希望它在第一行的跨度上循環,然後向下移動,循環遍歷第二行,然後向下移動,循環遍歷第三行,然後循環第四行,然後循環並執行整個事情再次。

感謝您的幫助。

回答

2

我猜你可以執行down()函數,當across()函數達到它的目的?

function across() { 
    var $active = $('.div .current'); 
    var $next = $active.next();  
    $next.addClass('current'); 
    $active.removeClass('current'); 
    if($next.length == 0) { 
     $(".div .bed:first-child").addClass('current'); 
     down(); 
    } 
} 

function down() { 
    var $active = $('#slideshow .active'); 
    var $next = $active.next();  
    $next.addClass('active'); 
    $active.removeClass('active'); 
    if($next.length == 0) { $("#slideshow .div:first-child").addClass('active'); } 
} 

$(function() { 
    setInterval(across, 500); 
}); 

FIDDLE

PS:不setInterval的使用字符串,referrence功能。

+0

*臉掌*非常感謝! – Starkers 2013-02-24 15:08:26

+0

看起來很明顯,但我們都去過那裏。相信我! – adeneo 2013-02-24 15:09:15

+0

哈哈再次感謝 – Starkers 2013-02-24 16:05:48