我正在使用Javascript / HTML对游戏进行10秒倒计时。此时代码在加载时运行。但是,问题是我无法将其链接到一个按钮,以便我可以点击代码。
function countdown(secs,elem){
var element= document.getElementById(elem);
element.innerHTML = "Time is running out.." +secs+ " seconds";
secs--;
var timer = setTimeout('countdown('+secs+' ,"'+elem+'")',1000);
if (secs<1) {
clearTimeout(timer);
element.innerHTML="<h1> GAME OVER!</h1>";
};
}
&#13;
<div class="clock">
<div id="status"></div>
<script type="text/javascript">countdown(10,"status");</script>
</div>
&#13;
我尝试了很多不同的东西,但似乎无法让它发挥作用。任何帮助都会很棒。感谢
答案 0 :(得分:0)
function countdown(secs, elem) {
var element = document.getElementById(elem);
element.innerHTML = "Time is running out.." + secs + " seconds";
secs--;
var timer = setTimeout('countdown(' + secs + ' ,"' + elem + '")', 1000);
if (secs < 1) {
clearTimeout(timer);
element.innerHTML = "<h1> GAME OVER!</h1>";
};
}
&#13;
<div class="clock">
<div id="status"></div>
<button onclick="countdown(10,'status');">Click Me</button>
</div>
&#13;
答案 1 :(得分:0)
试试这个,您可以按照此tutorial进行参考
$ tsc -v
message TS6029: Version 1.6.2
&#13;
function countdown(secs, elem) {
var element = document.getElementById(elem);
element.innerHTML = "Time is running out.." + secs + " seconds";
secs--;
var timer = setTimeout('countdown(' + secs + ' ,"' + elem + '")', 1000);
if (secs < 1) {
clearTimeout(timer);
element.innerHTML = "<h1> GAME OVER!</h1>";
};
}
&#13;