番茄时间js版

哈哈,写了一个

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    *{
        margin: 0;
        padding: 0;
    }
    #countDownArea{
        background-color: #000;
        font-size:32px;
        color:#fff;
    }
    </style>
</head>
<body>
<input type="button" value="start" id="start">
<input type="button" value="pause" id="pause">
<input type="button" value="reset" id="reset">
<input type="button" value="setDuration" id="setDuration">
<input type="text" id="duration" value="25">
<p >the duration is <span id="desc">25</span>min</p>
<p id="countDownArea"></p>
<script>
window.onload = function(){
    var oBtnStart = document.getElementById('start');
    var oBtnPause = document.getElementById('pause');
    var oBtnReset = document.getElementById('reset');
    var oTextDuration = document.getElementById('duration');
    var oBtnSetDuration = document.getElementById('setDuration');
    var oCountDownArea = document.getElementById('countDownArea');
    var oDesc = document.getElementById('desc');
    var defaultDuration = 25*60*1000;
    var timer = null;
    var now = 0;

    //you can set the duration manually
    oBtnSetDuration.onclick = function(){
        clearInterval(timer);
        defaultDuration = parseInt(oTextDuration.value)*60*1000;
        oCountDownArea.innerHTML = beautyTime(defaultDuration);
        oDesc.innerHTML = defaultDuration;
    }
    

    oBtnStart.onclick = function(){
        clearInterval(timer);
        now = Date.now();
        oCountDownArea.innerHTML = beautyTime(defaultDuration);
        timer = setInterval(function(){
            var newNow = Date.now();
            var timeDelay = defaultDuration - (newNow - now);
            if(timeDelay <= 0){
                clearInterval(timer);
                oCountDownArea.innerHTML = 0;
                return;
            }
            oCountDownArea.innerHTML = beautyTime(timeDelay);
        },500);
    }

    oBtnPause.onclick = function(){
        clearInterval(timer);
    }

    oBtnReset.onclick = function(){
        clearInterval(timer);
        defaultDuration = 25*60*1000;
        oCountDownArea.innerHTML = beautyTime(defaultDuration);
    }

    function beautyTime(time){
        var hour = parseInt(time/3600000);
        var min = parseInt((time%3600000)/60000);
        var sec = parseInt((time%60000)/1000);
        var other = time - hour*3600000 - min * 60000 - sec*1000;
        return hour+"hour-"+min+"min-"+sec+"sec-"+other;
    }

}   
</script>   
</body>
</html>

转载于:https://my.oschina.net/u/1792175/blog/598036

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值