问答详情
源自:8-6 取消计时器clearTimeout()

自己编的代码,计数器不能停止

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
	function kaishi(){
		alert("开始了!");
		document.getElementById("kaishi").innerHTML = "开始了!";	
	}
	function tiwen(){
		var daan = prompt("请输入内容",daan);	
		if(daan != null){
			document.getElementById("daan").innerHTML = daan;	
		}
	}
	function queren(){
		var queren = confirm("确定要修改"+document.getElementById("daan").innerHTML+"?");	
		if(document.getElementById("daan").innerHTML != "" && queren == true){
				document.getElementById("daan").innerHTML = "答案已经被修改!";
				document.getElementById("daan2").style.backgroundColor = "#000";
		}
	}
	function dianji(){
		document.getElementById("shubiao").innerHTML="按钮被点击";
	}
	function clock(){
    var time=new Date();          
    attime= time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
    document.getElementById("clock").innerHTML = "当前时间:"+attime;
  }
  var interval = setInterval("clock()",1000);
  var ii=0;
	function clock2(){
		document.getElementById("clock2").innerHTML = "计数器:"+ii;
		ii=ii+1;
		setTimeout("clock2()",1000);
		}
	var timeout = setTimeout("clock2()",1000);
	function stopCount(){
      clearTimeout(timeout);
  }
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="500" border="1">
    <tr>
      <td><input type="button" name="button" id="button" value="警告" onclick="kaishi()"/></td>
      <td id="kaishi"></td>
    </tr>
    <tr>
      <td><input type="button" name="button1" id="button1" value="提问" onclick="tiwen()"/></td>
      <td id="daan"></td>
    </tr>
    <tr>
      <td><input type="button" name="button2" id="button2" value="确认修改" onclick="queren()"/></td>
      <td id="daan2">&nbsp;</td>
    </tr>
    <tr>
      <td><input type="button" name="button3" id="button3" value="打开新窗口" onclick="javascript:window.open('test1_1.php','_blank','width=300;height=300');"/></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><input type="button" name="button4" id="button4" value="鼠标事件测试" onclick="dianji()"/></td>
      <td id="shubiao"></td>
    </tr>
    <tr>
      <td><script type="text/javascript">
	  	var date= new Date();
		document.write(date);
      </script></td>
      <td id="clock"></td>
    </tr>
    <tr>
      <td>
      <input type="button" name="button5" id="button5" value="停止↑" onclick="javascript:clearInterval(interval);" />
      <input type="button" name="button6" id="button6" value="停止→" onclick="stopCount()" />
      </td>
      <td id="clock2"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>


提问者:百度哥 2016-05-05 08:56

个回答

  • 百度哥
    2016-05-05 08:59:59

    http://img.mukewang.com/572a9aff0001dc2208190460.jpg计数器那里

  • 百度哥
    2016-05-05 08:57:23

    clock2() 的“计数器”那里,点击了按钮事件还是不能停止。