当前位置: 首页 > news >正文

前端实现简易 计时器

前端实现简易 计时器

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box {display: flex;flex-direction: column;align-items: center;}</style>
</head><body><div class="box"><h1 class="time">00:00:00</h1><div><button class="start_timing">开始计时</button><button class="stop_timing" disabled>停止计时</button><button class="reset_timing">重置计时器</button></div></div><script>const startTiming = document.querySelector('.start_timing')const stopTiming = document.querySelector('.stop_timing')const resetTiming = document.querySelector('.reset_timing')const time = document.querySelector('.time')let count = 0let timer = null// 核心代码const formatTime = (seconds) => {const hrs = Math.floor(seconds / 3600);const mins = Math.floor((seconds % 3600) / 60);const secs = Math.floor(seconds % 60);return [hrs.toString().padStart(2, '0'),mins.toString().padStart(2, '0'),secs.toString().padStart(2, '0')].join(':');};startTiming.addEventListener('click', () => {timer = setInterval(() => {count++const timeStr = formatTime(count)time.innerHTML = timeStr}, 1000)startTiming.disabled = truestopTiming.disabled = false})stopTiming.addEventListener('click', () => {clearInterval(timer)startTiming.disabled = falsestopTiming.disabled = true})resetTiming.addEventListener('click', () => {count = 0const timeStr = formatTime(count)time.innerHTML = timeStrclearInterval(timer)startTiming.disabled = falsestopTiming.disabled = true})</script>
</body></html>

具体功能再二次改造

在这里插入图片描述

http://www.xdnf.cn/news/332479.html

相关文章:

  • 多层PCB SMT贴装全流程指南:从物料准备到回流焊工艺控制
  • allegro出gerber时,单击Artwork并没有弹窗的问题
  • 数据分析案例(2)
  • 【AI落地应用实战】Amazon Bedrock 零门槛使用 DeepSeek-R1:在 Amazon Bedrock 上部署与调用的完整实践指南
  • 15_sysfsLinux内核模块
  • influxdb实战
  • CPU-GPU-NPU-TPU 概念
  • 统一返回JsonResult踩坑
  • 今日行情明日机会——20250507
  • 前端三大件--HTML
  • svn服务器迁移
  • BK精密电源操作软件 9130BA系列和手侧user manual
  • 文生图(Text-to-Image)的发展
  • 人工智能如何进行课堂管理?
  • 分布式爬虫去重:Python + Redis实现高效URL去重
  • 多账号管理、反追踪与自动化测试:我的浏览器实战笔记
  • string--OJ4
  • LeetCode106_从中序与后序遍历序列构造二叉树
  • 迈向AI辅助数据分析代码生成的透明性与知识共享
  • #黑马点评#(三)缓存穿透/雪崩/击穿
  • hadoop中的序列化和反序列化(1)
  • MySQL的information_schema在SQL注入中的关键作用与防御策略
  • 由浅入深谈Python书写规范
  • 【MySQL】-- 联合查询
  • Linux:进程控制1
  • 如何利用 QuickAPI 生成 PostgreSQL 样本测试数据:全面解析与实用指南
  • vue-qr生成的二维码增加下载功能
  • 【云备份】客户端开发
  • 百胜企业管理咨询:助力企业快速获得ecovadis认证
  • SecureCRT SFTP命令详解与实战