timer.html 4.38 KB
<!DOCTYPE HTML>
<html>

<head>
    <meta charset=utf-8>
    <title>yugi</title>
    <script src="../src/js/jquery-1.7.1.js"></script>
<!--     <script language="javascript">
    var se, m = 0,
        h = 0,
        s = 0,
        ss = 1;

    function second() {
        if ((ss % 100) == 0) {
            s += 1;
            ss = 1;
        }
        if (s > 0 && (s % 60) == 0) {
            m += 1;
            s = 0;
        }
        if (m > 0 && (m % 60) == 0) {
            h += 1;
            m = 0;
        }
        t = h + "时" + m + "分" + s + "秒" + ss + "毫秒"; //时分秒运算
        document.getElementById("showtime").value = t; //这有一个给id为showtime的input赋值的语句,可以实现动态计时。
        //其实所谓的动态计时,就是在很短的时间里不停给显示时间的地方更新数值,由于速度很快,这样计时器看起来时刻都在变化。但其实不是的,它从本质上还是静态的,这跟js的伪多线程原理是一样的。
        ss += 1;
    }

    function startclock() {
        se = setInterval("second()", 1);
    } //这个函数是要放到按钮的click事件上的
    function pauseclock() {
        clearInterval(se);
    } //这个函数是要放到按钮的click事件上的
    function stopclock() {
        clearInterval(se);
        ss = 1;
        m = h = s = 0;
    } //这个函数是要放到按钮的click事件上的
    </script>
 -->
<script>
    $(function(){
        var timer = {
            options: {
                timer: null,
                dom: null
            },
            init: function(dom){
                this.options.dom = dom;
                this.startTimer();
            },
            getTimer: function(){
                var _dom = this.options.dom;
                var t = 1,
                    h = 0,
                    m = 0,
                    s = 1;
                return function(){
                    console.log(_dom);
                    if (s > 0 && s % 60 == 0) {
                        m += 1;
                        s = 0;
                    }
                    if (m > 0 && m % 60 == 0) {
                        h += 1;
                        m = 0;
                    }
                    t = (h < 10 ? '0' + h : h) + ":" + (m < 10 ? '0' + m : m) + ":" + (s < 10 ? '0' + s : s);
                    _dom.html(t);
                    s += 1;
                }
            },
            startTimer: function(){
                var _getTimer = this.getTimer();
                this.options.timer = setInterval(_getTimer, 1000);
            },
            stopTimer: function(){
                clearInterval(this.options.timer);
                this.options.timer = null;
            }
        }
        var dom = $('#timer')
        timer.init(dom);

        $('.stopTimer').click(function(){
            timer.stopTimer();
        })
        
        // var t = 1,
        //     h = 0,
        //     m = 0,
        //     s = 1,
        //     timer;
        // var dom = $('#timer')
        // function second(dom){
            
        //     var _dom = dom;
        //     return function(){
        //         console.log(dom);
        //         if (s > 0 && s % 60 == 0) {
        //             m += 1;
        //             s = 0;
        //         }
        //         if (m > 0 && m % 60 == 0) {
        //             h += 1;
        //             m = 0;
        //         }
        //         t = (h < 10 ? '0' + h : h) + ":" + (m < 10 ? '0' + m : m) + ":" + (s < 10 ? '0' + s : s);
        //         _dom.html(t);
        //         s += 1;
        //     }
            
        // }
        // var _second=second(dom);
        // function startTimer() {
        //     timer = setInterval(_second, 1000);
        // }

        // function stopTimer() {
        //     clearInterval(timer);
        //     s = 1;
        //     m = h = 0;
        // }
        // // alert(dom)
        // startTimer()

    })
</script>
</head>

<body>
    <h1 id="timer"></h1>
    <button class="stopTimer">暂停</button>

    <input name="s" type="button" value="开始计时" onClick="startclock()">  
    <input name="s" type="button" value="暂停计时" onClick="pauseclock()">  
    <input name="s" type="button" value="停止计时" onClick="stopclock()">  
      
    <input name="showtime" style="color:#ff0000;width:200px;" id="showtime" type="text" value="0时0分0秒"> 
</body>

</html>