var x = 0;
var int = 1000; //1000ms interval
function Start(){
setInterval(function(){
console.log(x += 1); //increasing x value by 1 every int miliseconds
console.log("Interval: " + int); //outputting interval value (in my case, it is always 1000ms, i want it to be for example: 1000ms, 950ms, 900ms, 850ms, ... , 15ms, 10ms, 5ms, 0ms)
}, int); //how to increase/decrease this 'int' value gradually - (for example: 1000ms, 950ms, 900ms, 850ms, ... , 15ms, 10ms, 5ms, 0ms)
}
我的目标:逐渐将'x'值增加1(在开始时,'x'缓慢增加,随着它的增加,它开始越来越快地增加)
5 回答
一旦 Build 了计时器's time, it can' t就会被更改 . 解决方案是使用
setTimeout
而不是setInterval
,以便您可以使用新延迟 Build 新计时器 . 然后每个连续的计时器可以使用不同的时间:据我所知,你的想法是逐渐改变 Value . 我想出了一些算法 . 我通过
step value, which is becoming roughly 2 times smaller every time we get to the middle of the previous
top值更改
int` . 这是代码:希望这很有用 .
正如temmu所说,你不能 . 但是,每次创建一个新的间隔都是一个坏主意,因为这会导致一些严重的内存泄漏 .
改为使用
setTimeout
:使用setTimeout()而不是每次使用较低的
delay
值调用自身的递归方法 . 检查下一个示例:也可以通过重新启动函数来使用
setInterval
.