2015-04-12 20:15:25
方法一;
<script> var s=0; var timeout=0; status='点击按钮,开始计时'; function count(){ s++; time1.value=s; timeout=setTimeout('count();',100); } function onChange(){ if(timeout==0){ count(); time2.value='暂停计时'; status='点击按钮,暂停计时'; }else{ clearTimeout(timeout); timeout=0; time2.value='继续计时'; status='单击按钮,继续计时'; } } </script> </head> <body> <input name='time1'/><br/> <input type='button' name='time2' onclick='onChange();' value='开始计时'/> </body>
第二种方法:
<body> <input id='text1'/> <input type='button' id='jishi' value='开始计时' onclick='getTim();'/> <script> var jishi=null,text1=null,k=0,timer=null; function getTim(){ var jishi=document.getElementById("jishi"); var text1=document.getElementById("text1"); switch(jishi.value){ case '开始计时':window.status='单击按钮,开始计时'; case '继续计时': jishi.value='单击暂停'; window.status=''; timer=setInterval(function(){ text1.value=k; k++; },5);break; case '单击暂停': window.status='单击按钮,开始计时'; clearInterval(timer); jishi.value='继续计时';break; default:break; } } </script> </body>