用JS写了一个30分钟倒计时器

效果图

在这里插入图片描述

额外功能

  1. 左键单击计时器数字区,不显示或显示秒钟区。
  2. 左键双击计时器数字区,暂停或启动计时器。计时完毕,只能刷新页面启动计时器。
  3. 输入框可输入备注信息,输入框失去焦点或计时完毕后,时间戳附带备注信息会存入本地。
  4. 左键双击进度条区,可将导出历来使用计数器所处时间及备注信息。
  5. 两audio标签可填个人喜爱音频文件的路径作启动、结束时的音乐。

放码过来

<!DOCTYPE HTML>
<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"/><title>Countdown Timer</title><style type="text/css">input{padding-bottom: 0px;padding-top: 0px;border-top-width: 0px;border-left-width: 0px;border-right-width: 0px;font-size: 20px;width:100%;}</style></head><body><span id="numbers" style="white-space:nowrap;"></span><table id="table1" ><tr><td style="background-color:rgb(41, 74, 155);padding:3px;"></td><td></td></tr><tr><td style="width:100%;" colspan=2><input id="content"/></td></tr><tr><td style="width:100%;" colspan=2><canvas id="myCanvas" height="6">Your browser does not support the canvas element.</canvas></td></tr></table><audio id='music'><source src="music/Windows XP 启动.wav" type="audio/mpeg">Your browser does not support the audio tag.</audio><audio id='music2'><source src="music/Windows XP 关机.wav" type="audio/mpeg">Your browser does not support the audio tag.</audio><script type="text/javascript" >var timer = {initMinutes:30,restSeconds:0,minute:0,second:0,handle:0,stopFlag:false,startTime:0,content:"asdasd",coverFlag:false,setFontSize:function(){document.getElementById("numbers").style.fontSize = (window.innerWidth|| document.documentElement.clientWidth|| document.body.clientWidth) / 3 + "px"},refreshTable:function(){//刷新进度条//第一条进度条var table = document.getElementById("table1")var span = document.getElementById('numbers')//table2.style.width = table.style.width = span.offsetWidth + "px"var progress = 1if(this.restSeconds > 0)progress = this.restSeconds / (this.initMinutes * 60)document.querySelector('#table1 td:nth-of-type(1)').style.width = progress * 100 + "%"var td2 = document.querySelector('#table1 td:nth-of-type(2)')if (progress < 1){td2.style.width = (1 - progress) * 100 + "%"}else{td2.style.display = "none"}//第二条进度条,用画布实现var canvas = document.getElementById('myCanvas')canvas.width = span.offsetWidthvar ctx = canvas.getContext("2d")var rectWeight = progress * span.offsetWidthctx.clearRect(0, 0, span.offsetWidth, 20)ctx.fillStyle = "#FF0000"//console.log("rectWeight: " + rectWeight)//console.log(progress * span.offsetWidth)ctx.fillRect(0, 0, rectWeight, 20)},init:function(){this.startTime = Date.now()var span = document.getElementById('numbers')this.setFontSize()this.restSeconds = this.initMinutes * 60 this.minute = this.initMinutesvar obj = thisthis.handle = setInterval(function(){if(obj.stopFlag)returnif(obj.restSeconds > 0){span.innerHTML = "" + (obj.minute < 10 ? "0" + obj.minute : obj.minute) + ":" + (!obj.coverFlag ? (obj.second < 10 ? "0" + obj.second : obj.second) : "&nbsp;".repeat(4))if(obj.restSeconds > 0){obj.restSeconds -= 1}obj.minute =  Math.floor(obj.restSeconds / 60)obj.second =  obj.restSeconds - obj.minute * 60//刷新进度条obj.refreshTable()}else{span.innerHTML = "Time "window.clearInterval(obj.handle)document.getElementById("music2").play()//跑完后记录var content = document.getElementById("content").valueobj.markdownRecord(content)//不停地闪烁window.setInterval(function(){span.innerHTML = (span.innerHTML == "Time ")?"is up.":"Time "}, 5000)}}, 1000)document.getElementById("music").play()var numbers = document.getElementById("numbers")numbers.addEventListener("click", function(){obj.coverFlag = !obj.coverFlag})numbers.addEventListener("dblclick", function(){obj.stopFlag = !obj.stopFlag})document.getElementById('content').addEventListener("blur", function(){if(obj.restSeconds > 0)returnvar content = document.getElementById("content").valueif(this.content != content){this.content = contentobj.markdownRecord(content)}})document.getElementById('table1').addEventListener("dblclick", function(){console.log("timerHistory:")console.log(localStorage.getItem('timerHistory'))console.log("\n")obj.exportHistory()})},markdownRecord:function(content){var records = []var timerHistory = localStorage.getItem("timerHistory")if(timerHistory != null){records = JSON.parse(timerHistory)}var lastRecord = records[0]if(lastRecord && lastRecord.start == this.startTime){lastRecord.note = content}else{var history = {start:this.startTime,duration:this.initMinutes,note:content}records.unshift(history)//数组头插入元素			}var recordsJson = JSON.stringify(records)//将结果存入本地localStorage.setItem("timerHistory", recordsJson)console.log(records[0])console.log("Marked it Down.")},exportHistory:function(){var filename = 'record' + Date.now() +'.txt'var text = localStorage.getItem('timerHistory')this.exportFile(filename, text)},exportFile:function(filename, text){var element = document.createElement('a');element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));element.setAttribute('download', filename);element.style.display = 'none';document.body.appendChild(element);element.click();document.body.removeChild(element);}}window.onresize = function(){timer.setFontSize()timer.refreshTable()}//pausewindow.onclick = function(){//timer.stopFlag = !timer.stopFlag}//mainwindow.onload = function(){timer.init()	}</script></body>
</html> 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/445560.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

为什么高手离不了Linux系统?我想这就是理由!

通过本文来记录下我在Linux系统的学习经历&#xff0c;聊聊我为什么离不了Linux系统&#xff0c;同时也为那些想要尝试Linux而又有所顾忌的用户答疑解惑&#xff0c;下面将为你介绍我所喜欢的Linux系统&#xff0c;这里有一些你应该知道并为之自豪的事实。 这里你应该首先抛开W…

python学习实例(1)

# #1.2 计算机编程的基本概念 ## #1.2.2 从Python语言进入计算机语言的世界 ##<程序&#xff1a;例子1> def F(x,y):return(x*xy*y) print("F(2,2)",F(2,2)) print("F(3,2)",F(3,2))#<程序&#xff1a;例子2> def Pr():for i in range(0,10)…

用JS写一个电影《黑客帝国》显示屏黑底绿字雨风格的唐诗欣赏器

效果图 放码过来 <!DOCTYPE HTML> <html><head><meta http-equiv"Content-Type" content"text/html;charsetutf-8"/><title>Black Screen And Green Characters</title><style type"text/css">table…

python学习实例(2)

# #2.2 不同进制间的转换 ## #2.2.1. 二进制数转换为十进制数 ##<程序&#xff1a;2-to-10进制转换> binput("Please enter a binary number:") d0; for i in range(0,len(b)):if b[i] 1:weight 2**(len(b)-i-1)d dweight; print(d)#<程序&#xff1a;改…

元器件封装大全:图解+文字详述

先图解如下&#xff1a; 元器件封装类型&#xff1a; A.Axial  轴状的封装&#xff08;电阻的封装&#xff09;AGP &#xff08;Accelerate raphical Port&#xff09; 加速图形接口 AMR(Audio/MODEM Riser) 声音/调制解调器插卡BBGA&#xff08;Ball Grid Array&#xff09;…

python学习实例(3)

# #3.4 关于Python的函数调用 ## #3.4.2 Python函数入门 ##<程序&#xff1a;计算43*22> #函数f def f(x, y):return x*y*y#主函数部分 c4f(3, 2) print (c)# #3.4.3 局部变量(Local variables)与全局变量(Global variables) ##<程序&#xff1a;打印局部变量a和全局…

用JS写一个丐版《2048》小游戏

效果图 放马过来 <!DOCTYPE HTML> <html><head><meta http-equiv"Content-Type" content"text/html;charsetutf-8"/><title>2048</title><style type"text/css">.basic{height:80px;width:80px;back…

如何有效申请TI的免费样片

转自如何有效申请TI的免费样片 TI公司愿意为支持中国大学的师生们的教学、实验、创新实践、竞赛和科研项目&#xff0c;提供有限数量的免费样片。首先需要指出的是&#xff1a;所有的样片申请应该是诚实正当的&#xff0c;所有不恰当的申请&#xff08;包括不必要或多余的&…

python学习实例(4)

# #第四章的python程序 ## #4.1 简洁的Python ##<程序&#xff1a;Python数组各元素加1> arr [0,1,2,3,4] for e in arr:tmpe1print (tmp)## #4.2 Python内置数据结构 ## #4.2.1 Python基本数据类型 ##<程序&#xff1a;产生10-20的随机浮点数> import random f …

用Python批量生成字幕图片用于视频剪辑

说明 视频剪辑时需要为视频添加字幕&#xff0c;添加字幕方法之一&#xff1a;根据字幕文本文件批量生成透明底只有字幕内容的图片文件&#xff0c;如下图&#xff0c;然后将这些图片文件添加到视频剪辑软件轨道中。 于是用pillow这Python图片工具库执行本次批量生成工作。 …

关于接地:数字地、模拟地、信号地、交流地、直流地、屏蔽地、浮

除了正确进行接地设计、安装,还要正确进行各种不同信号的接地处理。控制系统中&#xff0c;大致有以下几种地线&#xff1a; &#xff08;1&#xff09;数字地&#xff1a;也叫逻辑地&#xff0c;是各种开关量&#xff08;数字量&#xff09;信号的零电位。 &#xff08;2&…

python学习实例(5)

# #5.1 计算思维是什么 ##<程序: 找假币的第一种方法> by Edwin Sha def findcoin_1(L):if len(L) <1:print("Error: coins are too few"); quit()i0while i<len(L):if L[i] < L[i1]: return (i)elif L[i] > L[i1]: return (i1)ii1print("All…

一个用LaTeX写长除法计算过程的示例

源码 \begin{array}{lr} & x1 \\ x1 \!\!\!\!\!\! & \overline{)x^2 2x 1} \\ & \underline{x^2\ \ x\ \ \ \ \ \ \ } \\ & x 1 \\ & \underline{x1} \\ & 0 \end{array}效果 x1x1⁣ ⁣ ⁣ ⁣ ⁣ ⁣)x22x1‾x2x‾x1x1‾0\begin{array}…

AltiumDesigner中PCB如何添加 Logo

AltiumDesigner中PCB如何添加 Logo 转载2015-10-29 00:07:55标签&#xff1a;it文化教育首先用到的画图软件&#xff0c;当然是大家熟悉的Altium Designer了&#xff0c;呵呵&#xff0c;相信很多人都用过这款画图软件吧&#xff08;现在电路设计一直在用&#xff09;&#xff…

python学习实例(6)

# #6.6 文件系统&#xff08;File System&#xff09; ## #6.6.3 Python中的文件操作 ##<程序&#xff1a;读取文件os.py> f open("./Task1.txt",r); fls f.readlines() for line in fls:line line.strip(); print (line) f.close()#<程序&#xff1a;读…

网络视频ts格式文件下载及将其合成单一视频文件

一些网站会将视频分割成n个ts文件。 用猫抓chrome插件&#xff0c;抓取index.m3u8&#xff0c;可得到众多ts文件下载地址。 可用迅雷打包下载ts文件以及index.m3u8文件&#xff0c;但有时会出现下载不了的情况&#xff0c;怀疑是请求报头的问题上。 若迅雷下载不了&#xff…

PCB布局,布线技巧总结

PCB布局 在设计中&#xff0c;布局是一个重要的环节。布局结果的好坏将直接影响布线的效果&#xff0c;因此可以这样认为&#xff0c;合理的布局是PCB设计成功的第一步。 布局的方式分两种&#xff0c;一种是交互式布局&#xff0c;另一种是自动布局&#xff0c;一般是在自动布…

python学习实例(7)

# #第8章 信息安全&#xff08;Information Security&#xff09;的python程序 ## #8.3 措施和技术 ## #8.3.1 密码学 ##非对称加密#<程序&#xff1a;把n分解成p*q> import math n 221 m int(math.ceil(math.sqrt(n))) flag 0 for i in range(2,m1,1):if n % i 0:pr…

什么是TTL电平、CMOS电平、RS232电平

工作中遇到一个关于电平选择的问题,居然给忘记RS232电平的定义了,当时无法反应上来,回来之后查找资料才了解两者之间的区别,视乎两年多的时间,之前非常熟悉的一些常识也开始淡忘,这个可不是一个好的现象.:-),还是把关于三种常见的电平的区别copy到这里.做加深记忆的效果之用.. …

RFI滤波器电路

RFI滤波器电路 最实用解决方案是通过使用一个差分低通滤波器在仪表放大器前提供 RF 衰减滤波器。该滤波器需要完成三项工作&#xff1a;尽可能多地从输入端去除 RF能量&#xff0c;保持每个输入端和地之间的 AC 信号平衡&#xff0c;以及在测量带宽内保持足够高的输入阻抗以避免…