简体   繁体   English

setInterval加速| Java脚本

[英]setInterval Speed Up | Javascript

I'm trying to gradually depreciate the interval in which the function inside a setinterval function is executed 我正在尝试逐渐贬低setinterval函数内部函数的执行间隔

Here's abstracted code for principle: 这是原理的抽象代码:

var speed = 100;
window.setInterval( speed-= 0.01 , speed);

Am I correct in believing the value for speed is taken once, on the first execution of setInterval and used, instead of being taken at every execution. 我相信速度值是在setInterval的第一次执行时使用一次并使用的,而不是在每次执行时都采用的,这是正确的吗? If so, how can I get around this? 如果是这样,我该如何解决? I verified this by setting the interval to 1000 and setting the "speed-=" to 999, and then printing the value for speed. 我通过将时间间隔设置为1000并将“ speed- =”设置为999,然后打印速度值来验证这一点。 The value jumped from 1000 to 1 but then kept going down by 999, and even after the value became negative the functions inside setinterval were executed every 1 second. 该值从1000跳到1,但随后一直下降999,即使该值变为负值,setinterval内部的函数也每1秒执行一次。

An example of recursive call of setTimeout with changing interval. 一个带有更改间隔的setTimeout递归调用的示例。

 function go() { var node = document.createElement('div'); node.innerHTML = speed.toFixed(2); document.body.appendChild(node); if (speed > 1) { setTimeout(go, speed); } speed *= 0.9; } var speed = 100; go(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM