一、时间对象

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()

  1. // 定时器 setTimeout();
  2. // 多长时间过后,执行一次
  3. var time1 = setTimeout(function() {
  4. console.log(1);
  5. }, 1000);
  6. // setInterval();
  7. // 每隔多长时间,就执行一次
  8. var num = 0;
  9. var time2 = setInterval(function() {
  10. num++;
  11. /*
  12. 定时器的返回值:此定时器是当前页面中的第几个定时器;
  13. clearTimeOut(time1); //清除定时器之后,time1还有返回值;
  14. time1=null; // 所以,清楚定时器之后,给定时器赋值为null
  15. */
  16. if (num>10) {
  17. clearInterval(time2);
  18. time2=null;
  19. }
  20. // 定时器是异步执行;代码是自上而下执行,并且先走同步(一个一个执行)再走异步(同时执行)。
  21. console.log(num);
  22. }, 1000)

三、时钟案例

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4. <title>时钟</title>
  5. <style>
  6. div {
  7. width: 300px;
  8. height: 50px;
  9. margin: 0 auto;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div class="time" id="time"></div>
  15. <script>
  16. var div = document.getElementById("time");
  17. var newTime = "";
  18. var week = ["周日", "周一", "周二", "周三", "周四", '周五', "周六"];
  19. function zero(num) {
  20. if (num<10) {
  21. return "0" + num;
  22. }
  23. return num;
  24. }
  25. var time1 = setInterval (function() {
  26. if (newTime.length != 0) {
  27. newTime = "";
  28. }
  29. var year = new Date().getFullYear();
  30. var y1 = year.toString();
  31. var mounth = new Date().getMonth() + 1;
  32. var date = new Date().getDate();
  33. var day = new Date().getDay();
  34. var hour = new Date().getHours();
  35. var minutes = new Date().getMinutes();
  36. var seconds = new Date().getSeconds();
  37. // newTime = year+ "年" + mounth + "月" + date + "日" + week[day] + " " + hour + "时" +minutes+ "分" + seconds + "秒";
  38. newTime = newTime.concat(year,"年",zero(mounth),"月",zero(date),"日"," ",zero(hour),"时",zero(minutes)," 分", zero(seconds),"秒");
  39. console.log(newTime);
  40. div.innerText = newTime;
  41. }, 1000)
  42. </script>
  43. </body>

四、倒计时案例

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4. <title>倒计时</title>
  5. <style>
  6. .main {
  7. width: 500px;
  8. height: 100px;
  9. background-color: cyan;
  10. margin: 0 auto;
  11. text-align: center;
  12. }
  13. .main p {
  14. height: 50px;
  15. line-height: 50px;
  16. margin: 0;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="main">
  22. <p>距离2020年11月20日 17时00分00秒 <b>还有</b></p>
  23. <div class="time" id = "time"></div>
  24. </div>
  25. <script>
  26. var div = document.getElementById("time");
  27. var endTime = new Date("2020-11-20 17:00:00");
  28. function reverseTime(endTime) {
  29. var reTime = "";
  30. timeStamp = endTime - new Date();
  31. var hour = Math.floor(timeStamp / 1000 / 60 / 60);
  32. var minutes = Math.floor((timeStamp - hour * 60 * 60 * 1000) / 1000 / 60);
  33. var seconds = Math.floor((timeStamp - hour * 60 * 60 * 1000 - minutes * 60 * 1000) / 1000);
  34. reTime = reTime.concat(hour, "时", minutes, "分", seconds, "秒");
  35. return reTime;
  36. }
  37. setInterval(function () {
  38. div.innerText = reverseTime(endTime);
  39. }, 1000)
  40. </script>
  41. </body>

五、抽奖案例

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4. <title>抽奖</title>
  5. </head>
  6. <body>
  7. <div class="box" id="box"></div>
  8. <script>
  9. var div = document.getElementById("box");
  10. var ary = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  11. function num() {
  12. var newStr = "";
  13. for (i = 0; i < 4; i++) {
  14. var index = Math.floor(Math.random() * 10);
  15. // if (newStr.length > 3) {
  16. // newStr = "";
  17. // }
  18. if (newStr.includes(ary[index])){
  19. i--;
  20. continue;
  21. }
  22. newStr = newStr.concat(ary[index]);
  23. }
  24. return newStr;
  25. }
  26. var timer = setInterval(function () {
  27. div.innerText = num();
  28. console.log( `div.innerText: ${div.innerText}` );
  29. // console.log( `num(): ${num()}` );
  30. }, 999)
  31. var timer2 = setTimeout(function () {
  32. clearInterval(timer);
  33. timer = null;
  34. clearInterval(timer2);
  35. timer = null;
  36. alert("中奖号码为 "+div.innerText);
  37. }, 10000)
  38. </script>
  39. </body>