JavaScript关于DOM操作的一些小问题(2)
题目:倒计时
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
#box{width: 200px;height: 200px;border: 1px solid green;margin: 50px auto;line-height: 200px;text-align: center;font-size: 100px;}
input{margin: 20px auto;display: block;}
</style>
</head>
<body>
<div id="box">300</div>
<input type="button" id="start" value="开始">
<input type="button" id="end" value="暂停">
</body>
<script>
// 倒计时
//获取页面元素
var obox = document.getElementById("box");
var ostart = document.getElementById("start");
var oend = document.getElementById("end");
//设置一个t用来每次执行计时器时清楚计时器
var t = "";
//开始按钮
ostart.onclick = function(){
//执行前清楚计时器
clearInterval(t);
t = setInterval(function(){
if(obox.innerHTML == 0){
clearInterval(t);
}else{
obox.innerHTML--;
}
},1000)
}
//结束按钮
oend.onclick = function(){
clearInterval(t);
}
</script>
</html>
浙公网安备 33010602011771号