문제

Maybe someone can help me here.

My game where you can see the issue:

http://www.acsu.buffalo.edu/~jamesber/GameOne.html#

I made a puzzle game, and included a restart game href link. It works how I want except if you press it multiple times in a row very fast, it can scramble the timer and refreshes the puzzle super fast. What I want is to put like a timer on the button where it can't be pressed for like 2 seconds once it has been pressed. I tried this:

    var new; //global variable.

    document.getElementById("restart").onclick=function(){ 
new = setTimeout(function() {
restartGame();
}, 2000);
clearTimeout(new); clearTimeout(timer); restartGame(); };

    <b><a href="#" id="restart" style="position: absolute; left: 950px; top: 65px;">New Puzzle</a><b>
도움이 되었습니까?

해결책

How about using display:none for 2 seconds like this:

document.getElementById("restart").onclick=function(){
document.getElementById("restart").style.display = 'none';
new = setTimeout(function() {
restartGame();
}, 2000);
clearTimeout(new); clearTimeout(timer); restartGame(); };

Then when restartGame() is called after the 2 second setTimeout delay, make it reappear using document.getElementById("restart").style.display = 'inline';

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top