设置操作中的间隔更改速度

setInterval change speed under action

本文关键字:速度 操作 设置      更新时间:2023-09-26
我不知道

如何在setInterval中的函数工作时更改速度。

在代码中:

var timeout, count = 0, speed = 5000;
                  $('#stage').mousedown(function() {
                    timeout = setInterval(function() {
                        speed = parseInt(speed / 1.3); // HERE I want change speed
                        create(speed); // Some Function
                    }, speed); // This speed, I don't know how to change
                });
                $('#stage').mouseup(function() {
                    count = 0;
                    clearInterval(timeout);
                });

这项工作,但速度是功能之外的常量 (5000)

非常感谢所有的帮助!

您必须使用命名函数才能将其传递到新的 setTimeout 函数中。

var speed = 5000;
var timer;
$('#stage').mousedown(function() {
    timer = setTimeout(handleTick, speed);
});
$('#stage').mouseup(function() {
    clearTimeout(timer);    
});
var handleTick = function () {
    speed = parseInt(speed / 1.3);
    timer = setTimeout(handleTick, speed);
};

我所知,您无法更改调用setInterval后的延迟。但是,您可以以递归方式调用setTimeout

var speed = 5000;
function doSomething() {
    console.log(speed); // prints from 5000 to 1000
    speed -= 1000;
};
setTimeout(function () {
    doSomething(); // changes the global "speed" var internally
    speed && setTimeout(arguments.callee, speed);
}, speed);