I want to run the top set Interval whenever I removed my finger from space but when I use key up event it only run that function one time and I Dont know how to use if/else when I have add Event Listener
setInterval(function (e) {
r--;
}, 10);
document.addEventListener("keydown", function (e) {
console.log(r);
if (e.keyCode === 32) {
setInterval(function (e) {
r++;
if (r > 240) {
r = 200;
}
}, 100);
}
});
I'm assuming you want to do the function for x amount of times, when space is pressed down. If so you might wanne look at the code below. This will do a function for 100 times.
var isDoingStuff = false;
var r = 0;
function repeat(func, times) {
func();
times && --times && repeat(func, times);
}
function yourFunction(){
r++;
if(r > 240){
r = 200;
}
}
function onKeyDown(e){
if (!isDoingStuff && e.keyCode === 32) {
isDoingStuff = true;
console.log(r);
repeat(yourFunction, 100);
isDoingStuff = false;
}
}
document.addEventListener("keydown", onKeyDown);