一、时间对象
new Date(); 是一个对象
- getFullYear();
- getMouth(); // 0-11
- getDate(); // 当前日期
- getDay(); // 值[0, 6] 0代表周日,1-6代表周一到周六
- getHours();
- getMinutes();
- getSeconds();
- getMilliseconds(); // 毫秒
- getTime(); // 时间戳(1970年到现在的毫秒差)
- toLocaleString(); // 本地时间 “2020/11/20 上午9:59:59”
- toLocaleTimeString(); // “上午9:59:59”
- toLocaleDateString(); // “2020/11/20”
二、定时器
setTimeOut() setInterval()
// 定时器 setTimeout();
// 多长时间过后,执行一次
var time1 = setTimeout(function() {
console.log(1);
}, 1000);
// setInterval();
// 每隔多长时间,就执行一次
var num = 0;
var time2 = setInterval(function() {
num++;
/*
定时器的返回值:此定时器是当前页面中的第几个定时器;
clearTimeOut(time1); //清除定时器之后,time1还有返回值;
time1=null; // 所以,清楚定时器之后,给定时器赋值为null
*/
if (num>10) {
clearInterval(time2);
time2=null;
}
// 定时器是异步执行;代码是自上而下执行,并且先走同步(一个一个执行)再走异步(同时执行)。
console.log(num);
}, 1000)
三、时钟案例
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>时钟</title>
<style>
div {
width: 300px;
height: 50px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="time" id="time"></div>
<script>
var div = document.getElementById("time");
var newTime = "";
var week = ["周日", "周一", "周二", "周三", "周四", '周五', "周六"];
function zero(num) {
if (num<10) {
return "0" + num;
}
return num;
}
var time1 = setInterval (function() {
if (newTime.length != 0) {
newTime = "";
}
var year = new Date().getFullYear();
var y1 = year.toString();
var mounth = new Date().getMonth() + 1;
var date = new Date().getDate();
var day = new Date().getDay();
var hour = new Date().getHours();
var minutes = new Date().getMinutes();
var seconds = new Date().getSeconds();
// newTime = year+ "年" + mounth + "月" + date + "日" + week[day] + " " + hour + "时" +minutes+ "分" + seconds + "秒";
newTime = newTime.concat(year,"年",zero(mounth),"月",zero(date),"日"," ",zero(hour),"时",zero(minutes)," 分", zero(seconds),"秒");
console.log(newTime);
div.innerText = newTime;
}, 1000)
</script>
</body>
四、倒计时案例
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>倒计时</title>
<style>
.main {
width: 500px;
height: 100px;
background-color: cyan;
margin: 0 auto;
text-align: center;
}
.main p {
height: 50px;
line-height: 50px;
margin: 0;
}
</style>
</head>
<body>
<div class="main">
<p>距离2020年11月20日 17时00分00秒 <b>还有</b></p>
<div class="time" id = "time"></div>
</div>
<script>
var div = document.getElementById("time");
var endTime = new Date("2020-11-20 17:00:00");
function reverseTime(endTime) {
var reTime = "";
timeStamp = endTime - new Date();
var hour = Math.floor(timeStamp / 1000 / 60 / 60);
var minutes = Math.floor((timeStamp - hour * 60 * 60 * 1000) / 1000 / 60);
var seconds = Math.floor((timeStamp - hour * 60 * 60 * 1000 - minutes * 60 * 1000) / 1000);
reTime = reTime.concat(hour, "时", minutes, "分", seconds, "秒");
return reTime;
}
setInterval(function () {
div.innerText = reverseTime(endTime);
}, 1000)
</script>
</body>
五、抽奖案例
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>抽奖</title>
</head>
<body>
<div class="box" id="box"></div>
<script>
var div = document.getElementById("box");
var ary = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
function num() {
var newStr = "";
for (i = 0; i < 4; i++) {
var index = Math.floor(Math.random() * 10);
// if (newStr.length > 3) {
// newStr = "";
// }
if (newStr.includes(ary[index])){
i--;
continue;
}
newStr = newStr.concat(ary[index]);
}
return newStr;
}
var timer = setInterval(function () {
div.innerText = num();
console.log( `div.innerText: ${div.innerText}` );
// console.log( `num(): ${num()}` );
}, 999)
var timer2 = setTimeout(function () {
clearInterval(timer);
timer = null;
clearInterval(timer2);
timer = null;
alert("中奖号码为 "+div.innerText);
}, 10000)
</script>
</body>