js setInterval - 逐渐增加速度 [英] js setInterval - increase speed gradually

查看:658
本文介绍了js setInterval - 逐渐增加速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使setInterval逐渐增加速度,例如从1000毫秒开始,然后在几秒钟内逐渐减少到40毫秒,每次1毫秒。

How can I make setInterval to increase speed gradually, like start from 1000 ms and than go down until 40 ms gradually in few seconds by 1 ms at a time.

有什么想法?

这是我的代码:

setTimeout(function() {bonustimer = setInterval(function() { handleTimer2(rushcount); }, 40);}, 1000);


handleTimer2 = function() {
if(rushcount === -1) {
clearInterval(bonustimer);      
} else {
$('.digit-wrapper').html(rushcount);
rushcount--;
}}


推荐答案

设置间隔可能不会'这就是你想要的,因为你最终会杀死它并在每次迭代时重做它。更容易使用setTimeout,可能是这样的:

Set interval probably wouldn't be what you want here, as you just end up killing it and redoing it on every iteration. Much easier to use setTimeout, possibly something like this:

(function () {
    var interval = 1001;
    timer = function() {
        --interval;
        //do your thing here

        if (interval >= 40) {
            setTimeout(timer, interval);
        }
    };
    timer();
})();

另请注意,如果您一次只将间隔减少一毫秒,从1000减少到40 ,经历所有这些迭代需要很长时间。您可以随时用其他公式替换 - interval ,例如 interval = interval * 0.9; (减少10每次迭代的百分比)或你想要的任何公式。

Also note that if you only decrease the interval by one ms at a time, from 1000 down to 40, it takes quite a while to go through all those iterations. You can always replace --interval by some other formula, like interval = interval*0.9; (to reduce by 10% each iteration) or whatever formula you want.

这篇关于js setInterval - 逐渐增加速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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