Bug with .animate()'s callback?
I'm trying to make some div to animate, then in the callback, call another function. Nothing hard. Here's the code:
function transfertMenu(){
var chosenOption = this.firstChild.firstChild.id;
$('#leftMenucontent').animate({
left:'0px'
}, 500, constructMenu());
$('#leftMenucontent > ul > li').remove();
chooseMenu(chosenOption);
}
and tried
function transfertMenu(){
var chosenOption = this.firstChild.firstChild.id;
$('#leftMenucontent').animate({
left:'0px'
}, 500, function() {
constructMenu();
});
$('#leftMenucontent >开发者_如何学C ul > li').remove();
chooseMenu(chosenOption);
}
The problem is that when I put an alert() in the callback (constructMenu) it works perfectly, the animation finishes, then the alerts pops up. When I enter a function, it starts at the beginning, before the animation completes. Is it a bug, or i'm doing something wrong?
You should remove ()
from callback function because otherwise it gets called immediately:
$('#leftMenucontent').animate({
left:'0px'
}, 500, constructMenu());
------problem --------^
Should be:
$('#leftMenucontent').animate({
left:'0px'
}, 500, constructMenu);
So specify your callback function as constructMenu
instead of constructMenu()
精彩评论