1

我不知道如何在 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);
                });

这项工作,但速度在功能之外是 const (5000)

非常感谢所有帮助!

4

2 回答 2

1

您必须使用命名函数才能将其传递给新的 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);
};
于 2013-09-22T13:33:15.353 回答
0

据我所知,您无法在调用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);
于 2013-09-22T13:39:03.590 回答