2012-10-17 173 views
2

我做了一个litle javascript。像这样:Javascript功能延迟

content.css('height', contentHeight, function() { 
    alert("test"); 
    $("body").scrollTo("#main", 600); 
}); 

我想在设置内容高度后执行警报。但是我做错了什么?

回答

1

.css()功能是instantanious。

content.css('height', contentHeight); 
alert("test"); 
$("body").scrollTo("#main", 600); 
0

没有'完成'处理程序等方法,如css()。它立即执行。

所以,你必须编写的代码如下:

content.css("height", contentHeight); 
alert("test"); 
$("body").scrollTo("#main", 600); 

但是,如果你需要设置高度后添加一些延迟,你可以使用setTimeout()

content.css("height", contentHeight); 
setTimeout(function() { 
    alert("test"); 
    $("body").scrollTo("#main", 600); 
}, 1000); // 1000 is number of milliseconds 
0

的功能jQuery的css方法意思是不同的:

content.css('height', function(){ 
    return contentHeight; 
}); 

内容是一个数组,代码abo ve为此数组中的所有元素设置高度。此功能的用法可能如下:

content.css('height', function(index){ 
    return index * contentHeight; 
}); 

在这种情况下,每个下一个元素的高度将按contentHeight值递增。

0

使用动画,完成时调用回调。

content.animate({ 
    height: contentHeight 
}, 0, function() { 
    alert("test"); 
    $("body").scrollTo("#main", 600); 
}); 
0

尝试使用

content.animate(
    {height : 100}, 
    5000, 
    function() { 
     //Animation complete. 
     alert("test"); 
     $("body").scrollTo("#main", 600); 
    } 
);