1、时间对象

1.1【作用】:Date 对象用于处理日期和时间。

1.2【创建时间对象】: new Date()获取系统当前时间

  1. var myDate=new Date(); //Tue Dec 24 2019 20:44:00 GMT+0800 (中国标准时间);
  2. typeof myDate;====>"object" // 获取到是一个对象,并不是字符串

1.3 时间对象相关属性和方法

  • getFullYear();获取年
  • getMonth();获取月 0到11 代表1月到12月
  • getDate();获取日期
  • getDay();星期几 (0—-6)代表周日到到周六
  • getHours();时
  • getMinutes();分
  • getSeconds();秒
  • getMilliseconds();毫秒
  • getTime();获取当前日期到1970年1月1号 00:00:00 之间的毫秒差
  • toLocaleString();// 获取到的是年月日,时分秒”2019/12/25 上午10:15:50”
  • toLocaleDateString();// 获取到是字符串的年月日,例如:”2019/12/25”
  • toLocaleTimeString();/ 获取到的是字符串的时分秒上午10:18:28

    2、案例:钟表

    ```javascript <!DOCTYPE html>

  1. 0操作:
  2. ```javascript
  3. <!DOCTYPE html>
  4. <html lang="en">
  5. <head>
  6. <meta charset="UTF-8">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  9. <title>Document</title>
  10. <style>
  11. #clock{
  12. height:50px;
  13. line-height: 50px;
  14. text-align: center;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="clock" id="clock">
  20. </div>
  21. <script>
  22. var clock = document.getElementById("clock");
  23. function getTime() {
  24. var time = new Date();
  25. var year = time.getFullYear();
  26. var month = time.getMonth() + 1;
  27. var date = time.getDate();
  28. var day = time.getDay();
  29. var hour = time.getHours();
  30. var minutes = time.getMinutes();
  31. var seconds = time.getSeconds();
  32. var week = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  33. var res = year + "年" + addZero(month) + "月" + addZero(date) + "日" + week[day] + " " + addZero(hour) + ":" + addZero(minutes) + ":" + addZero(seconds);
  34. return res;
  35. }
  36. function addZero(num) {
  37. return num < 10 ? "0" + num : num;
  38. }
  39. var res = getTime();
  40. clock.innerText = res;
  41. setInterval(function () {
  42. var res = getTime();
  43. clock.innerText = res;
  44. }, 1000)
  45. </script>
  46. </body>
  47. </html>

3、定时器:

3.1、setTimeOut

  1. 一定的时间后,去执行某些事情,是单次调用
  1. setTimeout(function(){
  2. alert("wasai!you are beautiful");
  3. },1000)

3.2、setInterval

间隔多少时间后,去执行某些事情,是多次调用

  1. setInterval(function(){
  2. console.log("我真美!")
  3. },1000)

3.3、定时器的参数说明

  1. // 定时器可以传递多个参数:
  2. // 1、执行的函数
  3. // 2、时间
  4. // 3、后面的参数就是执行函数在执行的时候传递的实参
  5. setTimeout(function(num,s,m){
  6. console.log(num,s,m);
  7. },1000,2,3,6);

3.4、定时器是有返回值的,返回值代表定时器处于当前页面中的第几个

  1. var time1=setTimeout(function(){
  2. console.log("wasai!you are beautiful");
  3. },1000)
  4. var time2=setInterval(function(){
  5. console.log("我真美!")
  6. },1000)
  7. var time3=setTimeout(function(){
  8. console.log("aa");
  9. },1000)
  10. console.log(time1)=====>1
  11. console.log(time1)=====>2
  12. console.log(time1)=====>3

3.5、定时器是异步任务,只要当咱们同步代码执行完毕之后,才能执行。

  1. setTimeout(function(){
  2. console.log("定时器");
  3. },1000)
  4. console.log("haha"

4、清除定时器的方法

  • clearTimeout
  • clearInterval ```javascript

var time1=setTimeout(function(){ console.log(‘1’) },1000)

clearTimeout(time1);

var time2=setInterval(function(){ console.log(“in”) },1000);

clearInterval(time2)

  1. <a name="NHXgI"></a>
  2. ## 5、练习
  3. 做一个抽奖程序,页面中有一个区域显示中奖人员的编号,在JS中写一段代码,要求每隔1秒中随机创建一个四位的数字(每一位数字的取值范围0-9),当10秒结束后,结束定时器,最后显示的四位数字即是中奖的号码
  4. ```javascript
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  11. <title>中奖</title>
  12. <style>
  13. #prize{
  14. width:200px;
  15. height:50px;
  16. border:1px solid green;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="prize" id="prize"></div>
  22. </body>
  23. </html>
  24. <script>
  25. function getCode(){
  26. var str="0123456789";
  27. var result="";
  28. for(var i=0;i<4;i++){
  29. var index=Math.floor(Math.random()*9);
  30. result+=str[index];
  31. }
  32. prize.innerHTML=result;
  33. return result;
  34. }
  35. var time1=setInterval(function(){
  36. getCode();
  37. },1000);
  38. setTimeout(function(){
  39. clearInterval(time1);
  40. },10000)
  41. </script>

倒计时案例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. body {
  14. background: darkslateblue;
  15. }
  16. #time {
  17. height: 100px;
  18. line-height: 100px;
  19. border: 1px dashed yellow;
  20. text-align: center;
  21. color: #fff;
  22. font-size: 26px;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="time">
  28. </div>
  29. <script>
  30. /*
  31. 倒计时:
  32. 目标时间:2021年12月14号
  33. 目标时间-现在的时候=还剩多少时间?
  34. 为了方便计算时间差,我们统一转成到1970年...的时间戳
  35. */
  36. function countDown(targetStr) {
  37. // 目标时间
  38. var targetTime = new Date(targetStr);
  39. // 现在的时间
  40. var nowTime = new Date()
  41. // 时间差,得出来的是毫秒
  42. var diffTime = targetTime - nowTime;
  43. // 把时间差换算成时、分、秒
  44. var hours = Math.floor(diffTime / (1000 * 60 * 60));
  45. var minutes = Math.floor((diffTime - hours * 1000 * 60 * 60) / (1000 * 60));
  46. var seconds = Math.floor((diffTime - hours * 1000 * 60 * 60 - minutes * 60 * 1000) / 1000);
  47. var result = formatter(hours) + "时" + formatter(minutes) + "分" + formatter(seconds) + "秒";
  48. return result;
  49. }
  50. function formatter(val) {
  51. return val = Number(val) < 10 ? "0" + val : val;
  52. }
  53. var res = countDown("2021-12-24 00:00:00");
  54. time.innerHTML = res;
  55. console.log(res)
  56. setInterval(() => {
  57. var res = countDown("2021-12-24 00:00:00");
  58. time.innerHTML = res;
  59. }, 1000);
  60. </script>
  61. </body>
  62. </html>