Reputation: 13
I don't know how I can change speed while function in setInterval works..
In code:
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);
});
This work but speed is outside function is const (5000)
Thanks a lot for all helps!
Upvotes: 1
Views: 1974
Reputation: 9291
You would have to use a named function in order to pass it into a new setTimeout function.
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);
};
Upvotes: 1
Reputation:
As far as I know, you can't change the delay after a call to setInterval
. However, you could call setTimeout
in a recursive manner :
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);
Upvotes: 0