<div id="time" style="border:solid 1px #ddd; display:inline-block; 50px; height:30px; vertical-align:middle"></div>
<input type="button" id="btn" value="停止">
<script type="text/javascript"> // 一个计时器, 点击按钮 让他 停一会, 5s后继续自动运行 var num=0; var data=1000; var timeout=false; function autoSide(){ num +=1; $("#time").html(num); } var t=setInterval(autoSide,data); $("#btn").click(function(){ t= clearInterval(t); var timeout = setTimeout(function(){ t= setInterval(autoSide,data); },5000); }) </script>
第二种方式:
<div id="time" style="border:solid 1px #ddd; display:inline-block; 50px; height:30px; vertical-align:middle"></div> <input type="button" id="btn" value="停止"> <script type="text/javascript"> var num=0; var data=1000; var timeout=false; function oNum(){ num++; $("#time").html(num); } function autoSide() { if (timeout) { setTimeout(autoSide, 5000); timeout = 0; } else { setTimeout(autoSide, 1000); oNum(); } } $("#btn").click(function () { timeout = 1; }) autoSide(); </script>