我是jQuery的新手,正在尝试在一段时间内填满一个div。当时间到了,我想要清除div -非常简单,对吧?
摘要:我点击一个按钮,div开始填充(在workPeriod上使用animate({ height: "100%"}, workPeriod * 1000),这是20秒。一旦它被填满,我希望div被清空。div填充时没有任何问题,一旦完成,大约需要5-10秒才能使用animate({ height: "0%"}, workPeriod * 5000)清除
$('#begin').click(function () {
var workPeriod = 20
var start = new Date($.now());
var end = new Date(start.getTime() + 1000 * workPeriod);
var workInterval = setInterval(function () {
var currentDif = Math.floor((new Date($.now()) - end) / 1000) * -1;
if (currentDif < 0) {
$('#session-time').animate({
height: '0%'
}, 0);
clearInterval(workInterval);
//I want the animation above to occur instantly but it's happening several seconds after
//the clearInterval call
} else {
$('#session-time').animate({
height: '100%'
}, workPeriod * 1000);
}
}, 1000);问:如何在div填满后立即清空,而不是等待5-10秒?
顺便说一句,我是jQuery的新手,在编程方面还是个初学者,所以任何建议都会被接受
发布于 2015-09-07 14:41:34
因此,如果您想在动画完成后调用另一个函数/动画,该功能已经内置于jQuery的animate函数中。
From the jQuery documentation:
$( "#clickme" ).click(function() {
$( "#book" ).animate({
opacity: 0.25,
left: "+=50",
height: "toggle"
}, 5000, function() {
// Animation complete.
});
});我试着让它与你上面发布的代码一起工作,但我不能让你的计时器东西工作。Nevertheless, I made this codepen, to show that the animation complete function does work when your complete function is another animation.
https://stackoverflow.com/questions/32431911
复制相似问题