知识点
1. 时间
A. 创建时间
-
使用内置构造函数创建时间对象 =>
var date = new Date()
比如
var date = new Date(); console.log(date);
B. 获取时间=>GET
比如
// 获取年 var yea = date.getFullYear(); // 获取月=> 注意: 获取到的是0~11,其中0表示1月份,11表示12月份 var mon = date.getMonth()+1 // 获取日 var dat = date.getDate(); // 获取时 var hou = date.getHours(); // 获取分 var min = date.getMinutes(); // 获取秒 var sec = date.getSeconds() // 获取星期 var day = date.getDay() document.write(`${yea}年${mon}月${dat}日-${hou}时${min}分${sec}秒__星期${day}`)
C. 设置时间_SET
比如
//设置年 date.setFullYear(2021) //设置月 date.setMonth(19)
D. 时间戳
-
时间戳就是从1970年1月1日0时0分0秒,格林威治,为计算机元年,时间戳的单位是毫秒值
-
获取时间戳
-
方式一 =>
var time1 = +new Date()
-
方式二 =>
var time = new Date(); var time2 = time.valueOf()
-
方式三 =>
var time = new Date(); var time3 = time.getTime()
-
设置时间戳
-
var time = +new Date("2008-8-8 0:0:0")
比如
//方式一 var time1 = +new Date(); console.log(time1); //方式二 var time = new Date(); var time2 = time.getTime() console.log(time2); //方式三 var time3 = time.valueOf() console.log(time3); //设置时间戳 var time4 = +new Date("2008-8-8 0:0:0") console.log(time4);
E. 案例
求距离下一次的国庆节还有多少天
//获取今天的时间戳 var date = new Date(); //获取今天的时间戳 var time1 = date.getTime(); //获取10月1日的时间戳 var time2 = +new Date("2023-10-1") //距离国庆节的时间 => 10月1号-今天 var diff = time2 - time1; //单位是毫秒 console.log("天", parseInt(diff / (1000 * 60 * 60 * 24))); console.log("时", parseInt(diff % (1000 * 60 * 60 * 24) / (1000 * 60 * 60))); console.log("分", parseInt(diff % (1000 * 60 * 60) / (1000 * 60))); console.log("秒", parseInt(diff / 1000 % 60));
2. 定时器
-
一次性定时器
语法
setTimeout(function(){ //当过了这个时间之后,那么就执行这个匿名函数里面的代码 },时间) //时间是一个毫秒值
-
循环定时器
语法
setInterval(function(){ //每过这个时间,就会执行匿名函数里面的代码 },时间) //时间是一个毫秒值
-
清除定时器
-
清除一次性定时器 =>
clearTimeout(一次性定时器的名字)
-
清除循环定时器 =>
clearInterval(循环定时器的名字)
比如
<button id="btn1">停止一次性定时器</button> <button id="btn2">停止循环定时器</button> <script> /* 一次性定时器 => setTimeout(function(){}) 清除一次性定时器 => clearTimeout(定时器的名字) 循环定时器 => setInterval(function(){}) 清除循环定时器 => clearInterval(定时器名字) */ var timer1 = setTimeout(function(){ console.log("我是一次性定时器"); },3000) var timer2 = setInterval(function(){ console.log("我是循环定时器"); },1000) //停止一次性定时器 btn1.onclick = function(){ //停止一次性定时器 clearTimeout(timer1) } //停止循环定时器 btn2.onclick = function(){ //停止循环定时器 clearInterval(timer2) } </script>
-
案例
倒计时
<button id="btn1">开始</button> <button id="btn2">结束</button><br> <p id="p">100</p> <script> //当我们点击btn1这个按钮之后,那么开始倒计时 /* 思路: 1. 对开始按钮有一个点击事件 2. 获取页面的文本值,将文本值转换成number类型 3. 需要一个定时器 => 循环定时器,在定时器里面数字-- */ var timer = null; //开始定时器 btn1.onclick = function(){ timer = setInterval(function(){ // var num = +p.innerHTML; // num --; // p.innerHTML = num; //简化 p.innerHTML -- },1000) } //停止定时器 btn2.onclick = function(){ clearInterval(timer) } </script>
3. 开关思想
比如
<button id="cj">陈锦</button> <button id="bl">碧落</button> <button id="zs">张三</button> <button id="ls">李四</button><br> <button id="en">上完厕所</button> <script> cj.onclick = function(){ wc("陈锦") } bl.onclick = function(){ wc("碧落") } zs.onclick = function(){ wc("张三") } ls.onclick = function(){ wc("李四") } var flag = true; //默认情况下,厕所门是打开的 //上厕所的函数 var timer = null;//定时器的名字 function wc(name){ /* 1. 先看门是否关闭,如果门是关闭的,那么就在外面等待...,如果门是开的,那么就进入到厕所中 2. 进来了之后,关门 */ if(!flag){ alert("当前厕所有人,请等待!") return; } flag = false;//进来之后,需要关门 timer = setInterval(function(){ console.log(name+"正在上厕所..."); },1000) } en.onclick = function(){ //清除定时器 clearInterval(timer) console.log("上完测试了,开门"); //上完厕所之后,需要开门 flag = true; } </script>
4. 同步异步
比如
/* 同步异步 JS的在执行代码的时候,优先执行同步代码,当同步代码执行完毕之后,那么再执行异步代码 常见的异步代码 定时器,Ajax,DOM */ console.log(111111111111); setTimeout(function(){ console.log("我是一次性定时器"); },100) console.log(222222222222); console.log(333333333333); console.log(444444444444);
5. 数学函数
-
使用的共同点: Math.xxx
-
数学函数
-
Math.random => 获取一个0~1之间的随机数(包含0~不包1)
-
Math.round => 四舍五入
-
Math.ceil => 向上取整
-
Math.floor => 向下取整
-
Math.PI => 求π
-
Math.pow(n,m) => 求n的m次方
-
Math.abs() => 求绝对值
-
Math.sqrt() => 开方
-
Math.max() => 求多个数字里面的最大值
-
Math.min() => 求多个数字里面的最小值
比如
// 1. 获取一个随机数,范围0~1,包含0不包含1 console.log(Math.random()); console.log(Math.random()); // 2. 四舍五入 console.log(Math.round(3.4));//3 console.log(Math.round(3.5));//4 console.log(Math.round(-3.4));//-3 console.log(Math.round(-3.5));//-3 console.log(Math.round(-3.6));//-4 // 3. 获取π console.log(Math.PI); // 4. 向上取整 console.log(Math.ceil(1.0001)); console.log(Math.ceil(-1.0001)); // 5. 向下取整 console.log(Math.floor(1.9999)); console.log(Math.floor(-1.9999)); // 6. 求幂次方 console.log(Math.pow(2,3)); console.log(Math.pow(3,3)); // 7. 求绝对值 console.log(Math.abs(5)); console.log(Math.abs(-5)); // 8. 开平方 console.log(Math.sqrt(9)); console.log(Math.sqrt(3)); // 9. 求多个数字里面的最大值 console.log(Math.max(1,2,3,4,5,6,7)); // 10. 求最小值 console.log(Math.min(1,2,3,4,5,6,7));
6. 进制
A. 认识进制
-
二机制: 逢二进一
-
八进制: 逢八进一
-
十进制: 逢十进一
-
十六进制: 逢十六进一
比如
二进制 八进制 十进制 十六进制 0 0 0 0 1 1 1 1 10 2 2 2 11 3 3 3 100 4 4 4 101 5 5 5 110 6 6 6 111 7 7 7 1000 10 8 8 1001 11 9 9 1010 12 10 A 1011 13 11 B 1100 14 12 C 1101 15 13 D 1110 16 14 E 1111 17 15 F 10000 20 16 10
B. 将十进制转换成其他进制
语法
变量.toString(n) //n表示的就是将十进制转换成其他进制
比如
var n = 255 //将这个十进制的16转换成二进制 console.log(n.toString(2)); var n = 255; console.log(n.toString(16)); var n = 240; console.log(n.toString(16));
C. 保留小数位
语法
变量.toFixed(n) //将数字保留n位小数
比如
var n = 1.23456; console.log(n.toFixed(2));//保留2位小数 console.log(n.toFixed(20));//保留20位小数 console.log(n.toFixed());//不保留小数