I have a task:
- Run animation on button click
- Run animation just once on holding a button
- Use just "onKeyDown" the only dom event that device supports
Here you can see the example how I did this:
let timer = null;
let isPressed = false;
const runAnimation = () => {
div.classList.remove('run-animation');
void div.offsetWidth;
div.classList.add('run-animation');
}
const fillDiv = () => {
if (!isPressed) {
isPressed = true;
runAnimation();
}
clearTimeout(timer);
timer = setTimeout(() => {
isPressed = false;
}, 50);
};
document.onkeydown = function() {
fillDiv();
}
But the problem here, that when you hold button, the animation plays twice
Is there some solution how to avoid second firing of run animation?