JavaScript暫停和繼續(xù)定時(shí)器的實(shí)現(xiàn)方法

字號(hào):


    這篇文章主要介紹了JavaScript暫停和繼續(xù)定時(shí)器的方法的相關(guān)資料,非常不錯(cuò),需要的朋友可以參考下
    對(duì)于JavaScript的定時(shí)器來說沒有嚴(yán)格意義上的暫停和重啟,只有清除停止,但是可以通過一些‘障眼法'實(shí)現(xiàn)
    allChild[index].onclick = function(){//當(dāng)點(diǎn)擊目標(biāo)且滿足下面的條件時(shí) 讓計(jì)時(shí)器時(shí)間暫停
    if(gamInt == true){
    $("#tu").css("display","block");
    //計(jì)時(shí)器暫停(清除定時(shí)器)
    clearInterval(countdownTimer);
    //延遲2s后游戲繼續(xù) 計(jì)時(shí)器暫停結(jié)束
    setTimeout(function(){
    // 移除所有的圖片
    removeImgFun();
    $("#tu").css("display","none");
    imgNum++;
    if(imgNum >= 10){
    imgNum = 10;
    }
    createImgFun();
    //定時(shí)器繼續(xù)接之前的時(shí)間增加 繼續(xù)播放(將之前的定時(shí)器重新引用一次 包括定時(shí)器的名字(必須要有 不然容易引起電腦死機(jī))完整引入)
    countdownTimer = setInterval(function (){
    countdownNum -= 0.01;
    //保留兩位小數(shù)
    countdownNum = countdownNum.toFixed(2);
    if (countdownNum <= 0){
    clearInterval(countdownTimer);
    //游戲結(jié)束
    alert("游戲結(jié)束!");
    //房祖名不能再點(diǎn)擊(分?jǐn)?shù)不再計(jì)數(shù))
    gamInt = false;
    }
    time.innerHTML = "倒計(jì)時(shí):" + countdownNum + "秒";
    },10);
    },2000)
    //計(jì)分
    scores += 1;
    score.innerHTML = "第" + scores + "關(guān)";
    if(scores > 8){
    $("#wc").css("display","block");
    $("#haha").css("display","none");
    $("#cj").on("click",award());
    $("#sm").on("click",explain());
    }
    }
    }
    以上所述是小編給大家介紹的JavaScript暫停和繼續(xù)定時(shí)器的方法,希望對(duì)大家有所幫助