利用HTML实现秒表功能

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Title</title>

<style>

.a{

background-image: url(https://pic.616pic.com/ys_bnew_img/00/44/96/SrHNIPCVGH.jpg);

width: 500px;

height: 500px;

/* border: 1px solid rgb(0, 255, 157); */

position:absolute;

left: 50%;

margin-left: -400px;

text-align: center;

line-height: 100px;

}

#showTime

{

margin-top: 200px;

margin-left: 100px;

color: rgb(1, 7, 12);

text-align: center;

width: 300px;

height: 60px;

font-size: 60px;

}

#startBn{

width: 60px;

height: 30px;

/* margin-top: 300px; */

}

#restBn{

width: 60px;

height: 30px;

}

</style>

</head>

<body>

<div class="a">

<div id="showTime">00:00:00</div>

<button id="startBn">启动</button>

<button id="restBn">复位</button>

</div>

<script>

//------------------

var time,showTime,startBn,restBn,pauseDate;

//布尔开关

var bool=false;

//暂停的累计时间

var pauseTime=0;

init();

function init() {

showTime=document.getElementById("showTime");

startBn=document.getElementById("startBn");

restBn=document.getElementById("restBn");

startBn.addEventListener("click",clickHandler);//开始按钮 ~ 暂停按钮

restBn.addEventListener("click",clickHandler);//复位按钮

setInterval(animation,16);

}

//转化时间函数

function animation() {

if(!bool) return;

//前时间减去上次开启时间减去暂停累计时间

var times=new Date().getTime()-time-pauseTime;

var minutes=Math.floor(times/60000);//毫秒转化为分钟

var seconds=Math.floor((times-minutes*60000)/1000);//已知分钟

// 将time减去分钟 除去1000得出 秒

var ms=Math.floor((times-minutes*60000-seconds*1000)/10);//

showTime.innerHTML=

(minutes<10 ? "0" +minutes : minutes)+":"

+(seconds<10 ? "0"+seconds :seconds)+":"

+(ms<10 ? "0"+ms : ms);

}

//点击时的事件

function clickHandler(e) {

e= e || window.event;

if(this===startBn){

bool=!bool;

if(bool){

this.innerHTML="暂停";

//如果我们上一次暂停时间是空,表示没有暂停过,因此,直接返回0

//如果上次的暂停时间是有值得,用当前毫秒数减去上次的毫秒数,这样就会得到暂停时间

pauseTime+=(!pauseDate ? 0 : new Date().getTime()-pauseDate);

if(time) return;

time=new Date().getTime();

return;//是为bool判断跳出

}

this.innerHTML="启动";

pauseDate=new Date().getTime();

return;//是为this是否等于startBn判断跳出

}

startBn.innerHTML="启动";

pauseTime=0;

pauseDate=null;

bool=false;

time=0;

showTime.innerHTML="00:00:00";

}

</script>

</body>

</html>

相关推荐
用户908324602733 分钟前
Spring AI + RAG + SSE 实现带搜索来源的智能问答完整方案
前端·后端
GISer_Jing7 分钟前
阿里开源纯前端浏览器自动化 PageAgent,[特殊字符] 浏览器自动化变天啦?
前端·人工智能·自动化·aigc·交互
清风徐来QCQ26 分钟前
js中的模板字符串
开发语言·前端·javascript
成都渲染101云渲染666631 分钟前
Houdini+Blender高效渲染方案(高配算力+全渲染器兼容)
前端·系统架构
SuperEugene1 小时前
Vue3 + Element Plus 表格实战:批量操作、行内编辑、跨页选中逻辑统一|表单与表格规范篇
开发语言·前端·javascript
极梦网络无忧1 小时前
基于 Vite + Vue3 的组件自动注册功能
前端·javascript·vue.js
Predestination王瀞潞1 小时前
5.4.3 通信->WWW万维网内容访问标准(W3C):WWW(World Wide Web) 协议架构(分层)
前端·网络·网络协议·架构·www
爱学习的程序媛2 小时前
【Web前端】优化Core Web Vitals提升用户体验
前端·ui·web·ux·用户体验
zabr2 小时前
花了 100+ 篇笔记,我整理出 了一套 AI Agent 工程完全指南
前端·后端·agent
软弹2 小时前
深入理解 React Ref 机制:useRef 与 forwardRef 的协作原理
前端·javascript·react.js