无法将javascript链接到html按钮

时间:2015-11-15 04:25:53

标签: javascript html button onclick

我正在使用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;
&#13;
&#13;

我尝试了很多不同的东西,但似乎无法让它发挥作用。任何帮助都会很棒。感谢

2 个答案:

答案 0 :(得分:0)

&#13;
&#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;
<div class="clock">
  <div id="status"></div>
  <button onclick="countdown(10,'status');">Click Me</button>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个,您可以按照此tutorial进行参考

&#13;
&#13;
$ 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;
&#13;
&#13;