<span id="aa"></span><br />
        <span id="bb"></span><br>
        <script type="text/javascript">
        function secondCounter1(defSec, func, dispObj) {
        
        document.getElementById(dispObj).innerHTML = defSec--;
        if(defSec < 0)
        {
            eval(func);
            window.setTimeout("secondCounter1("+document.getElementById(dispObj).getAttribute("defSec")+",\""+func+"\",\""+dispObj+"\")",1000);
        }
        else
            window.setTimeout("secondCounter1("+defSec+",\""+func+"\",\""+dispObj+"\")",1000);
        }
        document.getElementById("aa").setAttribute("defSec","60");
        secondCounter1(60,"alert('aa')","aa");
        document.getElementById("bb").setAttribute("defSec","30");
        secondCounter1(30,"alert('bb')","bb");
        </script>如何用两个按钮分别停止这两个倒计时,再点击时又继续执行 

解决方案 »

  1.   

    <script type="text/javascript">
    var run=false;//定一个一个全局变量function secondCounter1(defSec, func, dispObj) {
         if(run)return;//在function 第一行加入这一句
    <input type="button" onclick="run=true;" value="计时">
    <input type="button" onclick="run=false;" value="暂停">
    加入两个按钮
      

  2.   

    <!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>
    </head><body>
    <span id="aa"></span><br />
      <span id="bb"></span><br>
      <input id="stop" type="button" value="stop" />
      <input id="start" type="button" value="start" />
      <script type="text/javascript">
      var fun = false;
      function secondCounter1(defSec, func, dispObj) {
      if(fun){return;}
      document.getElementById(dispObj).innerHTML = defSec--;
      if(defSec < 0)
      {
      eval(func);
      window.setTimeout("secondCounter1("+document.getElementById(dispObj).getAttribute("defSec")+",\""+func+"\",\""+dispObj+"\")",1000);
      }
      else
      window.setTimeout("secondCounter1("+defSec+",\""+func+"\",\""+dispObj+"\")",1000);
      }
      document.getElementById("aa").setAttribute("defSec","60");
      secondCounter1(60,"alert('aa')","aa");
      document.getElementById("bb").setAttribute("defSec","30");
      secondCounter1(30,"alert('bb')","bb");
      
      document.getElementById('stop').onclick = function(){
    fun = true;  
      };
      document.getElementById('start').onclick = function(){
    fun = false;  
    secondCounter1(+document.getElementById('bb').innerHTML,"alert('bb')","bb");
    secondCounter1(+document.getElementById('aa').innerHTML,"alert('aa')","aa");

      };
      </script></body>
    </html>
      

  3.   

    额楼主跟楼上的都没听说过有个叫clearTimeout的东东么?
      

  4.   

    <span id="aa"></span><br />
      <span id="bb"></span><br>
      <script type="text/javascript">
      var x 
      var y
      function secondCounter1(defSec, func, dispObj) {
       
      document.getElementById(dispObj).innerHTML = defSec--;
      if(defSec < 0)
      {
      eval(func);
      window.setTimeout("secondCounter1("+document.getElementById(dispObj).getAttribute("defSec")+",\""+func+"\",\""+dispObj+"\")",1000);
      }
      else
        if(dispObj=="aa")
        return window.setTimeout("x =secondCounter1("+defSec+",\""+func+"\",\""+dispObj+"\")",1000);
         else
      return window.setTimeout("y =secondCounter1("+defSec+",\""+func+"\",\""+dispObj+"\")",1000);
      }
      document.getElementById("aa").setAttribute("defSec","60");
      secondCounter1(60,"alert('aa')","aa");
      document.getElementById("bb").setAttribute("defSec","30");
      secondCounter1(30,"alert('bb')","bb");
      
      
      function fn1(o)
      {  if(x)
     {
      x= window.clearTimeout(x) 
      o.value="第一个开始"
     }
     else
     {
    o.value="第一个暂停"
      secondCounter1( parseInt(document.getElementById("aa").innerHTML),"alert('aa')","aa");
     }
      }
        function fn2(o)
      {  if(y)
     {
     o.value="第二个开始"
      y= window.clearTimeout(y) 
     }
     else
     {
    o.value="第二个暂停"
      secondCounter1( parseInt(document.getElementById("bb").innerHTML),"alert('bb')","bb");
     }
      }
      </script>
      
      <input type="button" onclick="fn1(this)" value="第一个暂停" />
      <input type="button" onclick="fn2(this)" value="第二个暂停" />