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>
運行截圖:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM