曹永思

导航

JavaScript 简单定时器原理

  <input type="button" value="开始计时!" onclick="timedCount()">
    <input type="text" id="txt">
    <input type="button" value="停止计时!" onclick="stopCount()">
    <script type="text/javascript">
        var c = 0;
        var t;
        timedCount();
        function timedCount() {
            document.getElementById('txt').value = c;
            c = c + 1;
            t = setTimeout("timedCount()", 1000);
        }

        function stopCount() {
            clearTimeout(t);
        }

    </script>

posted on 2012-12-09 14:29  曹永思  阅读(193)  评论(0编辑  收藏  举报