如何加快倒数? [英] How to speed up the count down?

查看:128
本文介绍了如何加快倒数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Javascript进行基本的倒数,其中,倒数从0开始,然后一直持续到24,因为这就是倒数的想法,我想在24结束。这是代码:

I am working on a basic countdown using Javascript where, the countdown starts from 0 and then ends till 24 as thats the idea of the countdown, I want to end it at 24. Here's the code:

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

}

现在的事情是,如果您注意这一点,倒计时发生得非常快,这是预期的效果。但是问题是,是否有一种方法可以产生平滑的缓动型效果,即倒数开始缓慢,然后在结束时加快速度?如何实现这种效果?

Now the thing is that if you notice this, the countdown happens very fast, this is the intended effect. However the question is this, Is there a way to have a smooth easing type effect, where the countdown starts slowly and then speeds up by the end ? How to achieve that effect ?

感谢您的回复。

编辑:这是小提琴,以了解倒计时的动作并获得更深刻的见解。

Here is the fiddle, to see the countdown in action and to gain a deeper insight.

推荐答案

使用仅运行一次的超时,然后添加额外的时间,然后再次运行超时,直到达到24。

Use a timeout which runs only once then add extra time and run the timeout again until you reach 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);

  }

}

这篇关于如何加快倒数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆