js之制作网页计时器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>网页计时器</title>
</head>

<body>
<input type="button" id="b1" value="阅览时间">

<script>
//判断是否小于0,如果小于,加60//
     function f(t) {
        if (t<0){
            a=t+60;
            return a
        }
        else {
            return t
        }
    }
//获取起始时间//
    var n_time_H=new  Date().getHours();
    var n_time_m=new  Date().getMinutes();
    var n_time_s=new  Date().getSeconds();
//获取变量//
    b1Elm=document.getElementById('b1');
    b1Elm.onclick=function () {
//获取当前时间//
     var c_time_H=new  Date().getHours();
    var c_time_m=new  Date().getMinutes();
    var c_time_s=new  Date().getSeconds();
//进行时间的计算//
    var temp_h=f(c_time_H-n_time_H);
    var temp_m=f(c_time_m-n_time_m);
    var temp_s=f(c_time_s-n_time_s);

//进行判断与输出//
    if (!temp_h)
        {
            if(!temp_m){
                alert('阅览时间:'+temp_s+'秒')
            }
            else{
                alert('阅览时间:'+temp_m+'分'+temp_s+'秒')
        }
        }
    else {
        alert('阅览时间:'+temp_h+'小时'+temp_m+'分'+temp_s+'秒')
    }
    }
</script>
</body>
</html>
运行截图:

 

 

转载于:https://www.cnblogs.com/98WDJ/p/10673537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值