无法以明确的间隔停止

时间:2014-05-04 16:09:05

标签: javascript date timer setinterval clearinterval

尽管我已经为我的两个setIntervals使用了全局变量,但计时器永远不会停止计数,有时它会在没有理由的情况下向后计数。如果您需要更多代码或代码需要更多解释,请评论我。在代码的最后我有clearIntervals函数。还有clearInt1& clearInt2是全局变量。感谢

        var buttonS = document.createElement("button");
        buttonS.id = "score";
        buttonS.value = "score";
        buttonS.textContent = "score";
        document.body.appendChild(buttonS);
        appendBreak();
        var buttonT = document.createElement("button");
        buttonT.id = "Time";
        buttonT.value = "Time";
        buttonT.textContent = "Time";
        document.body.appendChild(buttonT);
        clearInt1=setInterval(function(){updateScore()}, 5);
        clearInt2=setInterval(function(){updateTime()},5);

        function updateScore(){
       document.getElementById('score').innerHTML = "Score = Right moves: "+rightMoves+", Wrong moves: "+wrongMoves;
    }

    function updateTime(){
      var theNow=new Date();
      var minN=theNow.getMinutes();
      var secN=theNow.getSeconds();
      var minS=startDate.getMinutes();
      var secS=startDate.getSeconds();
      var min = minN-minS;
      var sec = secN- secS;
      document.getElementById('Time').innerHTML = "Time elapsed = Minutes: "+min+", Seconds: "+sec;
    }

    function onCl1 (id, value) {
        clearInt2=setInterval(function(){updateTime()},5);
      console.log(id, value);
        clicks++;

        if(clicks == 2) {

          if(id != previousId && value == previousValue) {
            clicks =0;
            rightMoves=rightMoves+1;
            clearInt1=setInterval(function(){updateScore()}, 500);
            alert("yeah");
            var obx = document.getElementById(id);
            var oby = document.getElementById(previousId);

            obx.disabled=true;
            oby.disabled=true;



            if(rightMoves==2){
                var stopD= new Date();
                clearInterval(clearInt1);
                var minS= startDate.getMinutes();
                var secS= startDate.getSeconds();
                var minE= stopD.getMinutes();
                var secE = stopD.getSeconds();
                var min1 = minE- minS;
                var sec1 = secE-secS;
                clearIntervals();
                alert("Congratulations! Right moves "+rightMoves+" , Wrong moves "+wrongMoves+" Time elapsed : Minutes "+min1+" Seconds "+sec1);
                clearIntervals();
                appendBreak();
                var buttonR = document.createElement("button");
                buttonR.id = "R";
                buttonR.value = "Play again!";
                buttonR.textContent = "Play again!";

                document.body.appendChild(buttonR);
                buttonR.addEventListener("click",function(){location.reload()});


            }    


          }else{
            clicks =0;
            wrongMoves=wrongMoves+1;
            clearInt1=setInterval(function(){updateScore()}, 500);
            alert("bad move! Think again!");
          }
        }
         else {

          previousId = id;
          previousValue = value;

        }

        function clearIntervals(){
          clearInterval(clearInt1);
          clearInterval(clearInt2);
        }
   }

0 个答案:

没有答案