【发布时间】:2015-11-15 16:56:33
【问题描述】:
目前正在制作一个基本游戏。想法:在设定的时间内点击尽可能多的图像。我为计时器和点击计数器编写了一些基本代码。我遇到的问题是,在计时器运行到零并提醒“游戏结束”后,玩家仍然可以单击图像并添加更多分数。
//Start button hidden after click
function hideBtn() {
document.getElementById("toggle").style.visibility="hidden";
}
//Countdown begins, goes to zero, alerts "game over"
function countdown(secs, elem) {
var element = document.getElementById(elem);
element.innerHTML = "Time: " + secs + " seconds";
secs--;
var timer = setTimeout('countdown(' + secs + ' ,"' + elem + '")', 1000);
if (secs <0) {
clearTimeout(timer);
alert("Game OVER!");
//element.innerHTML = "<h1> GAME OVER!</h1>";
};
}
//Click counter linked to multiple images
function clickdone() {
var hero = document.getElementById("hero");
var counter = document.getElementById("counter");
var count = counter.innerHTML;
count++;
counter.innerHTML = count;
}
<!--Linking countdown() to html-->
<div class="clock">
<div id="status"></div>
<button id="toggle" onclick="countdown(10,'status'), hideBtn()";>START</button>
</div>
<!--Example of one of the clickcounter images being linked to clickdone() and not counting more than one click-->
<div>
<img id="hero" src="images/img1.jpg" style="height:100px; width:100px;"
onclick="clickdone(); this.onclick=null;"/>
</div>
<!--click counter to html-->
<div class="statPanel">
<span id="counter">0</span>
</div>
我正在寻找链接 countdown() 和 clickdone() 的帮助,以便当计时器/countdown() 达到零并提醒“游戏结束”时,clickdone() 变为非活动状态,但仍会显示总点击次数。
【问题讨论】:
标签: javascript html button settimeout