為啥我這個(gè)stop 按鈕只有第一次有效 而start 一直有效
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定時(shí)器</title>
<style type="text/css">
#clock{background:red #339border:2px dashed blue }
h1{text-align:center }
h1{color:red}
.aa{color:blue}
</style>
</head>
<body>
<h1>顯示時(shí)間</h1>
<script type="text/javascript">
?// var attime;
? function clock(){
var time=new Date(); ? ? ? ? ?
? ? attime=time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
? ? document.getElementById("clock").value = attime;
?}
?var i=setInterval(clock,1000);
?function b()
?{var i=setInterval(clock,1000)}
?function c()
?{
var cc=clearInterval(i,1000)
}
? </script>
<form>
<input type="button" id="clock" size="50" ?value="clock()"/>
</form>
<p class="aa">我有一所房子,面朝大海春暖花開(kāi)。</p>
<p>喂馬 劈柴 周游世界</p>
<input type="button" value="start" onClick="b()">
<input type="button" value="stop" onClick="c()">
</body>
</html>
2018-03-25
2017-09-05
大神寫(xiě)的,很好用
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計(jì)時(shí)器</title>
<script type="text/javascript">
? var num=0;
? var i;
? function startCount(){
? ? document.getElementById('count').value=num;
? ? num=num+1;
? ? i=setTimeout("startCount()",1000);
? }
? flag=0;
? function stopCount(){
? ? ? if(flag==0){
? ? ? ? ? flag=1;
? ? ? ? ? document.getElementById('but').value="Stop";
? ? ? startCount();
? ? ? }else{
? ? ? ? ? clearTimeout(i);
? ? ? ? ? flag=0;
? ? ? ? ? document.getElementById('but').value="Start";
? ? ? }
? }
? function ce(){
? ? ? if(flag==0){
? ? ? document.getElementById('count').value="清除完畢!";
? ? ? num=0;
? ? ? setTimeout("document.getElementById('count').value=num",1000);
? ? ? }
? }
</script>
</head>
<body>
? <form>
? ? <input type="text" id="count" />
? ? <input type="button" id="but" value="Start" onclick="stopCount()" />
? ? <input type="button" value="Clear" ?onclick="ce()" />
? </form>
</body>
</html>
2017-08-06
你有兩個(gè)設(shè)定計(jì)時(shí)器的方法而且返回值都賦給了i第一個(gè)是一開(kāi)始啟動(dòng)的,第二個(gè)是一直在執(zhí)行的,你需要把function b里的變量定義到function b函數(shù)的上邊換一個(gè)變量名,就是這樣
2017-08-03
var i=setInterval('clock()',1000);