




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第用JS写了一个30分钟倒计时器的实现示例前端页面倒计时功能在很多场景中会用到,如很多秒杀活动等,本文主要介绍了用JS写了一个30分钟倒计时器的实现示例,感兴趣的可以了解一下
!DOCTYPEHTML
html
head
metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/
titleCountdownTimer/title
styletype="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
spanid="numbers"/span
tableid="table1"
tr
td/td
td/td
/tr
tr
tdcolspan=2
inputid="content"/
/td
/tr
tr
tdcolspan=2
canvasid="myCanvas"height="6"
Yourbrowserdoesnotsupportthecanvaselement.
/canvas
/td
/tr
/table
audioid='music'
sourcesrc="music/WindowsXP启动.wav"type="audio/mpeg"
Yourbrowserdoesnotsupporttheaudiotag.
/audio
audioid='music2'
sourcesrc="music/WindowsXP关机.wav"type="audio/mpeg"
Yourbrowserdoesnotsupporttheaudiotag.
/audio
scripttype="text/javascript"
vartimer={
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(){
//进度条
vartable=document.getElementById("table1")
varspan=document.getElementById('numbers')
//刷新进度条
//table2.style.width=
table.style.width=span.offsetWidth+"px"
varprogress=1
if(this.restSeconds0)
progress=this.restSeconds/(this.initMinutes*60)
document.querySelector('#table1td:nth-of-type(1)').style.width=progress*100+"%"
vartd2=document.querySelector('#table1td:nth-of-type(2)')
if(progress1){
td2.style.width=(1-progress)*100+"%"
}else{
td2.style.display="none"
}
varcanvas=document.getElementById('myCanvas')
canvas.width=span.offsetWidth
varctx=canvas.getContext("2d")
varrectWeight=progress*span.offsetWidth
ctx.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()
varspan=document.getElementById('numbers')
this.setFontSize()
this.restSeconds=this.initMinutes*60
this.minute=this.initMinutes
varobj=this
this.handle=setInterval(function(){
if(obj.stopFlag)
return
if(obj.restSeconds0){
span.innerHTML=""+(obj.minute10"0"+obj.minute:obj.minute)+":"+
(!obj.coverFlag(obj.second10"0"+obj.second:obj.second):"nbsp;".repeat(4))
if(obj.restSeconds0){
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()
//跑完后记录
varcontent=document.getElementById("content").value
obj.markdownRecord(content)
//不停地闪烁
window.setInterval(function(){
span.innerHTML=(span.innerHTML=="Time")"isup.":"Time"
},5000)
}
},1000)
document.getElementById("music").play()
varnumbers=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.restSeconds0)
return
varcontent=document.getElementById("content").value
if(this.content!=content){
this.content=content
obj.markdownRecord(content)
}
})
document.getElementById('table1').addEventListener("dblclick",function(){
console.log("timerHistory:")
console.log(localStorage.getItem('timerHistory'))
console.log("\n")
obj.exportHistory()
})
},
markdownRecord:function(content){
varrecords=[]
vartimerHistory=localStorage.getItem("timerHistory")
if(timerHistory!=null){
records=JSON.parse(timerHistory)
}
varlastRecord=records[0]
if(lastRecordlastRecord.start==this.startTime){
lastRecord.note=content
}else{
varhistory={
start:this.startTime,
duration:this.initMinutes,
note:content
}
records.unshift(history)//数组头插入元素
}
varrecordsJson=JSON.stringify(records)
//将结果存入本地
localStorage.setItem("timerHistory",recordsJson)
console.log(records[0])
console.log("MarkeditDown.")
},
exportHistory:function(){
varfilename='record'+Date.now()+'.txt'
vartext=localStorage.getItem('timerHistory')
this.exportFile(filename,text)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 硕士论文全攻略
- 济南幼儿师范高等专科学校《大数据隐私与数据安全》2023-2024学年第二学期期末试卷
- 苏州市相城区2025年初三周考生物试题一含解析
- 山西省晋中市太古区2025年四下数学期末质量检测模拟试题含解析
- 山西省名校2025届高三下-期末联考历史试题试卷含解析
- 内蒙古医科大学《传感器原理与应用》2023-2024学年第二学期期末试卷
- 宁波工程学院《中级日语听说》2023-2024学年第二学期期末试卷
- 江苏省苏州工业园区星澄校2025届初三3月第一次模拟语文试题含解析
- 山西省运城重点达标名校2024-2025学年初三下学期第五次重点考试数学试题含解析
- 江西省上饶市2025届四年级数学第二学期期末经典模拟试题含解析
- 劳务公司与公司合作协议书
- qw-zl03洁净室区环境监测作业指导书
- 不动产登记信息查询授权委托书
- 医院知情同意书模板
- 人工智能赋能金融保险
- 中国商业航天研究报告
- 《电力工程》PPT精品课程课件全册课件汇总
- 红色卡通风全国助残日PPT模板
- 楷书钢笔字帖(三字经)
- SimMarketing营销模拟实验室操作篇
- 金星星座查询对照表
评论
0/150
提交评论