javascript定時器完整實例

字號:


    這篇文章主要介紹了javascript定時器完整實現(xiàn)方法,以實例形式分析了定時器的嵌套調(diào)用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    本文實例講述了javascript定時器完整實現(xiàn)方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
    代碼如下:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>定時器</title>
    <script type="text/javascript">
    var timer = null;
    function begin() {
    var date = new Date();
    var current_time = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate() + ' ';
    current_time += date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
    document.getElementById('current_time').value = current_time;
    timer = setTimeout('begin()', 1000);
    }
    function end() {
    clearTimeout(timer); //清除掉定時器對象
    }
    </script>
    </head>
    <body>
    <input type="text" name="current_time" id="current_time"/><input type="button" value="開始" onclick="begin();">
    <input type="button" value="結(jié)束" onclick="end();"/><br/>
    </body>
    </html>
    希望本文所述對大家的javascript程序設(shè)計有所幫助。