⽤JS写了⼀个30分钟倒计时器的实现⽰例
前端页⾯倒计时功能在很多场景中会⽤到,如很多秒杀活动等,本⽂主要介绍了⽤JS写了⼀个30分钟倒计时器的实现⽰例,感兴趣的可以了解⼀下
<!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" ></span>
<table id="table1" >
<tr>
<td ></td>
<td></td>
</tr>
<tr>
<td colspan=2>
<input id="content"/>
</td>
</tr>
<tr>
<td 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.documentElement.clientWidth
|| document.body.clientWidth) / 3 + "px"
},
refreshTable:function(){
//进度条
var table = ElementById("table1")
var span = ElementById('numbers')
//刷新进度条
//table2.style.width =
table.style.width = span.offsetWidth + "px"
var progress = 1
stSeconds > 0)
progress = stSeconds / (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 = ElementById('myCanvas')
canvas.width = span.offsetWidth
var ctx = Context("2d")
var rectWeight = 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 = w()
var span = ElementById('numbers')
this.setFontSize()
this.minute = this.initMinutes
var obj = this
this.handle = setInterval(function(){
if(obj.stopFlag)
return
stSeconds > 0){
span.innerHTML = "" + (obj.minute < 10 ? "0" + obj.minute : obj.minute) + ":" +
(!verFlag ? (obj.second < 10 ? "0" + obj.second : obj.second) : " ".repeat(4)) stSeconds > 0){
}
obj.minute = Math.stSeconds / 60)
obj.second = stSeconds - obj.minute * 60
//刷新进度条
}else{
span.innerHTML = "Time "
window.clearInterval(obj.handle)
//跑完后记录
var content = ElementById("content").value
obj.markdownRecord(content)
//不停地闪烁
window.setInterval(function(){
span.innerHTML = (span.innerHTML == "Time ")?"is up.":"Time " }, 5000)
}
}, 1000)
var numbers = ElementById("numbers")
numbers.addEventListener("click", function(){
})
numbers.addEventListener("dblclick", function(){
obj.stopFlag = !obj.stopFlag
})
return
var content = ElementById("content").value
t != content){
obj.markdownRecord(content)
}
})
console.Item('timerHistory'))
console.log("\n")
})
},
markdownRecord:function(content){
var records = []
var timerHistory = Item("timerHistory")
if(timerHistory != null){
records = JSON.parse(timerHistory)
}
var lastRecord = records[0]
if(lastRecord && lastRecord.start == this.startTime){
< = 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' + w() +'.txt'
var text = Item('timerHistory')
},
exportFile:function(filename, text){
var element = ateElement('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();
veChild(element);
}
}
timer.setFontSize()
}
//pause
/
/timer.stopFlag = !timer.stopFlag
}
//main
timer.init()
js教程removechild}
</script>
</body>
</html>
到此这篇关于⽤JS写了⼀个30分钟倒计时器的实现⽰例的⽂章就介绍到这了,更多相关JS 倒计时器内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论