<!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>
运行截图: