我正在使用Javascript进行基本的倒数,其中,倒数从0开始,然后直到24结束,因为那是倒数的想法,我想在24结束。这是代码:
var count=0;
var counter=setInterval(timer, 50); //1000 will run it every 1 second
function timer()
{
count=count+1;
if (count >= 24)
{
clearInterval(counter);
//counter ended, do something here
document.getElementById("countdown").innerHTML=24 ;
return;
}
//Do code for showing the number of seconds here
document.getElementById("countdown").innerHTML=count ; // watch for spelling
}
现在的问题是,如果您注意到这一点,倒计时会很快发生,这就是预期的效果。但是问题是,有没有一种方法可以产生平滑的缓动型效果,即倒数开始缓慢,然后在倒数结束之前加速呢?如何达到那个效果?
感谢您的答复。
编辑:这是the fiddle,以查看倒计时的动作并获得更深入的了解。
最佳答案
使用仅运行一次的超时,然后添加额外的时间,然后再次运行超时,直到达到24。
var count=0;
var ms = 200;
var step = 5;
var counter=setTimeout(timer, ms); //1000 will run it every 1 second
function timer()
{
count=count+1;
if (count <= 24)
{
//Do code for showing the number of seconds here
document.getElementById("countdown").innerHTML=count ; // watch for spelling
ms = ms - step;
counter = setTimeout(timer, ms);
}
}