开发者

Bug with .animate()'s callback?

开发者 https://www.devze.com 2023-01-31 16:52 出处:网络
I\'m trying to make some div to animate, then in the callback, call another function. Nothing hard. Here\'s the code:

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()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号