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; usesetTimeout()
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
2 Answers
Reset to default 3Set 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
版权声明:本文标题:javascript - js setInterval - increase speed gradually - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745046324a2639385.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论