2012-10-14 86 views
0

我有一個運行兩個函數的onclick,我希望第一個函數能夠運行並完成,並在完成時啓動下一個函數。我把它設置像現在這種權利,他們似乎在同一時間運行:有一個功能的jquery在啓動另一個功能之前完成

$("#animate_1").click(function() { 
update_1_close(); 
welcome_open(); 
}); 

這是兩個功能,我不想把它們結合在一起,我有這幾條,並結合他們將結束最多是非常複雜的

function update_1_close() { 
$("#update_1").animate({ 
    "right": "175px", 
    "width": "160px" 
}, 450); 
$("#update_1_caption").animate({ 
    "bottom": "0px" 
}, 450); 
$("#update_1_img").animate({ 
    "height": "107px", 
    "width": "160px" 
}, 450); 
} 

function welcome_open() { 
$("#welcome_back_1").animate({ 
    "top": "345px" 
}, 450); 
$("#welcome_header_1").animate({ 
    "top": "35px", 
    "left": "20px" 
}, 450); 
$("#welcome").animate({ 
    "height": "270px" 
}, 450) 
$("#stick_figures").animate({ 
    "top": "215px" 
}, 450); 
$("#update_1").animate({ 
    "top": "465px", 
    "right": "175px" 
}, 450); 
$("#update_2").animate({ 
    "top": "465px" 
}, 450); 
$("#events").animate({ 
    "top": "645px" 
}, 450); 
$("#directions").animate({ 
    "top": "645px" 
}, 450); 
} 
+0

你能發佈2函數update_1_close和welcome_open? – Ben

回答

1

他們不能在同一時間運行 - 但是,他們可能沒有有機會和它們之間刷新瀏覽器中運行的一個又一個的權利。

我不知道這兩個函數做的,但你可能需要更多的東西是這樣的:

$("#animate_1").click(function() { 
    update_1_close(); 
    window.setTimeout(welcome_open, 1000); 
}); 

這將運行update_1_close,然後,一秒鐘後,運行welcome_open

2

可以傳遞welcome_open作爲update_1_close的參數,並在完成後執行它。由於您使用的是jquery動畫,請查看

您正在尋找「完整」參數。

function update_1_close(callback) { 
    $("#update_1").animate({ 
     "right": "175px", 
     "width": "160px" 
    }, 450, callback); 
    $("#update_1_caption").animate({ 
     "bottom": "0px" 
    }, 450); 
    $("#update_1_img").animate({ 
     "height": "107px", 
     "width": "160px" 
    }, 450); 
} 


update_1_close(welcome_open); 

否則,如果你知道update_1_close需要完成,你可以使用setTimeout的時間(但要小心,因爲你可以用神奇數字無處不在結束了,這似乎使已經發生)

相關問題