admin管理员组

文章数量:1410705

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.

Any thoughts?

This is my code:

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


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

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.

Any thoughts?

This is my code:

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


handleTimer2 = function() {
if(rushcount === -1) {
clearInterval(bonustimer);      
} else {
$('.digit-wrapper').html(rushcount);
rushcount--;
}}
Share edited Apr 11, 2014 at 6:21 Speedwheelftw asked Apr 11, 2014 at 6:12 SpeedwheelftwSpeedwheelftw 3936 silver badges19 bronze badges 6
  • 5 It's not practical to use setInterval() for this; use setTimeout() to get better control. – Ja͢ck Commented Apr 11, 2014 at 6:14
  • Also, explain "in few seconds by 1ms at a time", because that would mean it takes 1000 + 999 + 998 + ... + 40ms to reach 40ms :) – Ja͢ck Commented Apr 11, 2014 at 6:15
  • I have a timer, and at the end of the level it counts down the time bonus remaining, I have it setuped at 40 ms, and I want to start counting down the remaining seconds more slow at the begning and than to increase more fast until 0. But I don't know how to make the interval ms dynamic. – Speedwheelftw Commented Apr 11, 2014 at 6:18
  • Then you should probably take fixed percentages off the current time, e.g. 20% reduction at each step should take care of that. – Ja͢ck Commented Apr 11, 2014 at 6:20
  • 2 It would probably look beautiful ;-) – Ja͢ck Commented Apr 11, 2014 at 6:25
 |  Show 1 more ment

2 Answers 2

Reset to default 3

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();
})();

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.

I guess the code by @OldGeeksGuide just stops when the interval reaches 40. Needs a little improvement ;)

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

        interval = interval < 40 ? 40 : interval;
        setTimeout(timer, interval);
    };
    timer();
})();

Then you could maybe need to stop this loop by wrapping the setTimeout with some if condition ;)

本文标签: javascriptjs setIntervalincrease speed graduallyStack Overflow