I've got a mousedown event with a setinterval. I want the time of intervals to be variable. So the first one is 500, the second one 500/2 = 250, etc. Any tips?
$plus.mousedown(function(e) {
increment(20)
timeout = setInterval(function(){
increment(20)
}, 500);
});
$(document).mouseup(function(){
clearInterval(timeout);
return false;
});
Cheers!
EDIT: sorry for the ambiguity. I want the time of interval to change during the mousedown. So while the mousedown is being performed the intervaltime should change. So not by every single mouse click but with every continuous click, and then reset it again.
You can't really do this with
setInterval()
unless you keep clearing for a delay change, so you might as well write a wrapper aroundsetTimeout()
to accomplish something similar:To use:
This solution does not depend on jQuery:
You can add
console.log((new Date).getTime(), 20);
to the incrementAndWait method to see the numbers going on the console. Something fun to play with :)