var button = document.getElementById('button'); var coin = document.getElementById('coin'); var degs = Math.floor(Math.random() * (15000 - 2000) + 2000); function timer(){ coin.style.transform = "rotateY(" + degs + "deg)"; } button.addEventListener('click', timer); 

onclick works only once, how to fix it?

    1 answer 1

    The timer function is called for each click. But the real problem is the value of the variable degs , which does not change. Generate it directly when clicking:

     function timer() { var degs = Math.floor(Math.random() * (15000 - 2000) + 2000); coin.style.transform = "rotateY(" + degs + "deg)"; } 
    • Thank you, earned) - Roman Panichev