欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JS简易计时器

程序员文章站 2022-05-31 10:50:12
...

JS简易计时器

涉及功能:

  1. 动态加载下拉框
  2. ES6拼接html字符串
  3. 定时器的设置和清除
  4. str.padStart(targetLength [, padString])

问题总结:

  1. JS字符串拼接:传统拼接用“ ” 和 + 来拼接;ES6用 ` (反引号,不是单引号!!!)标识,用${ }引用变量,{ }可引用表达式(对象,数组,函数等)。
  2. padStart(2, ‘0’) 字符串补全长度。padStart()用于头部补全,padEnd()用于尾部补全。

JS简易计时器

var hourChoose = document.getElementById("hour");
var minChoose = document.getElementById("min");
var secChoose = document.getElementById("sec");

var timer = 0;


//动态加载下拉框
function setSelect(el, num) {
	for (var i = 0; i < num; i++) {
		el.innerHTML += `<option value ="${i}">${i}</option>`;  //` `ES6拼接html字符串,用${i}替换了+'i'+
	}
}

setSelect(hourChoose, 24);
setSelect(minChoose, 60);
setSelect(secChoose, 60);



var startBtn = document.querySelector(".btn_start");
var continueBtn = document.querySelector(".btn_continue");
var pauseBtn = document.querySelector(".btn_pause");
var resetBtn = document.querySelector(".btn_reset");

startBtn.onclick = function() {
	//获取值
	var hour = hourChoose.value;
	var min = minChoose.value;
	var sec = secChoose.value;
	// console.log(hour,min,sec);
    //计算总时间
	var totalNum = hour * 3600 / 1 + min * 60 / 1 + sec / 1;
	
    //设置定时器
	timer = setInterval(function() {
		//时间结束时清除定时器,否则值为负
		if (totalNum == 0) {
			clearInterval(timer);
			alert("⏲计时结束!!!");
			return;
		}
		totalNum--;
		rederTime(totalNum);
	}, 1000)
}

var time_hour=document.getElementById('time_hour');
var time_min=document.getElementById('time_min');
var time_sec=document.getElementById('time_sec');

continueBtn.onclick=function(){
	var hour=time_hour.innerHTML;
	var min=time_min.innerHTML;
	var sec=time_sec.innerHTML;
	var totalNum = hour * 3600 / 1 + min * 60 / 1 + sec / 1;
	
	
	timer = setInterval(function() {
		if (totalNum == 0) {
			clearInterval(timer);
			alert("⏲计时结束!!!");
			return;
		}
		totalNum--;
		rederTime(totalNum);
	}, 1000)
}

pauseBtn.onclick=function(){
	clearInterval(timer);
}

resetBtn.onclick=function(){
	clearInterval(timer);
	rederTime(0);

}

//设置事件渲染数据
function rederTime(num) {
	//转换时分秒   str.padStart(targetLength [, padString])用另一个字符串填充当前字符串,填充从当前字符串的开始(左侧)
	var hour = parseInt(num / 3600).toString().padStart(2, '0');
	var min = parseInt((num - hour * 3600) / 60).toString().padStart(2,'0');
	var sec = parseInt(num % 60).toString().padStart(2, '0');

	time_hour.innerHTML = hour;
	time_min.innerHTML = min;
	time_sec.innerHTML = sec;
}

html代码:

<body>
		<div id="home">
			<select id="hour">
			</select>
			<label></label>
			
			<select id="min">
			</select>
			<label></label>
			
			<select id="sec">
			</select>
			<label></label>
			
			<button class="btn_start">开始</button>
			<button class="btn_pause">暂停</button>
			<button class="btn_continue">继续</button>
			<button class="btn_reset">清零</button>
		</div>
		<div id="box">
			<div id="time_hour">00</div>
			<div id="point">:</div>
			<div id="time_min">00</div>
			<div id="point">:</div>
			<div id="time_sec">00</div>
		</div>
		<script src="01.js" type="text/javascript" charset="utf-8"></script>
	</body>

css代码:

#home{
	display: flex;
	justify-content: center;
	margin-top: 10%;
	border-radius: 5px;
}
#hour{
	width: 100px;
	height: 30px;
	padding: 6px 12px;
}
#min{
	width: 100px;
	height: 30px;
}
#sec{
	width: 100px;
	height: 30px;
}
label{
	font-size: 20px;
	margin-right: 15px;
}
.btn_start{
	width: 100px;
	height: 30px;
	border-radius: 5px;
	margin-left: 20px;
	margin-right: 5px;
	color:white;
	background-color: aquamarine;
	
}
.btn_reset{
	width: 100px;
	height: 30px;
	border-radius: 5px;
	margin-left: 10px;
	color:white;
	background-color: #FF557F;
}
.btn_continue{
	width: 100px;
	height: 30px;
	border-radius: 5px;
	margin-left: 10px;
	color: wheat;
	background-color: aqua;
	}
.btn_pause{
	width: 100px;
	height: 30px;
	border-radius: 5px;
	margin-left: 10px;
	color:wheat;
	background-color: aqua;
}

#box{
	 display: flex;
	 justify-content: center;
	 margin-top: 100px;
}
#box>div{
	font-size: 200px;
	color: #ff557f;
}

相关标签: 笔记