javascript定时器如何暂停及继续

发布时间:2019年08月06日 阅读:104 次

javascript定时器如何暂停及继续

// html

<div>当前时间为:<span id="timeNow"></span> </div>

<button id="timeBegin">计时开始</button>

<button id="timeEnd">计时结束</button>

<button id="timeClear">计时清除</button>

 

// Javascript

<script type="text/javascript">

    //定义初始值 计时器

    var count = 0;

    var timer = "";

 

    //开始计时

    function BeginTime(){

        var beginBtn = document.getElementById("timeBegin");

        beginBtn.onclick = function(){

            timer = setInterval(function(){

                count ++ ;

                document.getElementById("timeNow").innerHTML = count / 100;

            },10)

        }

    }

 

    //结束计时

    function EndTime(){

        var endBtn = document.getElementById("timeEnd");

        endBtn.onclick = function(){

            clearInterval(timer);

        }

    }

image.png

 

    //计时清除

    function ClearTime(){

        var clearBtn = document.getElementById("timeClear");

        clearBtn.onclick = function(){

            document.getElementById("timeNow").innerHTML = "";

        }

    }

 

    BeginTime();

    EndTime();

    ClearTime();

</script>

 

//  The Harder You Work, The Luckier You Will Be. Jensonhui.com

以上就是javascript定时器如何暂停及继续,希望这篇文章对您有所帮助!如有什么需要补充的欢迎到我们的网站进行补充留言!http://www.javascriptedu.com

 


Tag:javascript定时器如何暂停及继续 javascript定时器
相关文章

发表评论: