2012-10-14 89 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的时间(但要小心,因为你可以用神奇数字无处不在结束了,这似乎使已经发生)

相关问题