js内置对象
window里三大模块:history location document
内置对象
Date Math String Array
window里都有哪些对象???
window.alert(); 弹框
window.confirm();带确认,取消的弹框
window.prompt();带输入文本框的弹框
大量的事件:on*** ……
history
documnet
等等
最为重要的就是:window里面三大计时器
setInterval setTimeout requestAnimationFrame
setInterval()
setInterval() // 循环计时器 延迟多长时间去反复执行
两种写法:1.
setInterval(function () {
console.log(1);
}, 1000);
2.字符串函数
function showlog(){
console.log(1);
}
setInterval("showlog()",1000);
函数写在外面,调用名称而已,本质没有什么区别。
清除计时器 clearInterval();
清除计时器的返回值为number,这个number仅为返回值,本身并无意义。
//设定计时器
/*setInterval(function () {
console.log(1);
}, 1000);*/
/* function showlog(){
console.log(1);
}
setInterval("showlog()",1000);*/
//清除计时器返回的值为number
/*var count = 0;
var timer = setInterval(function () {
count++;
console.log(count);
if (count >= 10) {
console.log(timer);
//关闭计时器
clearInterval(timer);
}
}, 1000);*/
setTimeout()
setTimeout()是一次性计时器 也可以通过递归, 制作出循环计时器
settimeout 改装循环计时器
//函数的递归
var count = 0;
var timer=null;
showtime();
function showtime() {
count++;
console.log(count);
if(count>=10)
{
clearTimeout(timer);
return;
}
timer=setTimeout("showtime()", 1000);
}
requestAnimationFrame()
在浏览器放小或者切换选项卡的时候 会暂停。用法同上。
但是取消此计时器,只能用cancelAnimationFrame();
loop();
function loop()
{
console.log(1);
//根据电脑屏幕的fps 16ms 刷新一次 1000/60
window.requestAnimationFrame(loop);
}
location
loction 显示当前页面的地址的相关信息
//代码跳转路径使用下面代码
location.href="https://www.baidu.com";
history
一般,history都写在标签里。
<a href="javascript:history.go(-1)">倒退</a>
<a href="javascript:history.go(1)">前进</a>
<a href="./前进页面.html">前进页面</a>
<a href="./倒退页面.html">后退页面</a>
history 当前网页的历史记录
// console.log(history);
//
// window.history.forward();前一页
// window.history.back();后一页
Date对象
需要实例化,new,直接实例化完是本地时间。
* set** get***2 to***
* 1.设置时间
* */
/*var time=new Date("2019 8-13 13:42");
console.log(time);*/
//2019,8,13,13,42 方式的赋值 月份上面有+1情况
/*var time=new Date(2019,8,13,13,42);
console.log(time);*/
var time=new Date();
/*time.setDate(20)//日
time.setFullYear(2020)//设置年
time.setHours(0)//小时
time.setMilliseconds(0)//毫秒
time.setMinutes(0)//分
time.setMonth(0)//月
time.setSeconds(0)//秒*/
/*time.setTime()//设置当前时间到1970年0点的总毫秒数
time.setYear()//设置 119 1900--- 119 2019*/
//getMonth 获取的月 币正常月 少1 +1
//getTime 当前时间的总毫秒数
//console.log(time.getTime());
//和set一致的get方法获取时间
console.log(time.toDateString());
console.log(time.toISOString());
console.log(time.toLocaleDateString());
console.log(time.toLocaleString());
console.log(time.toLocaleTimeString());
console.log(time.toTimeString());
console.log(time.toUTCString());
Math对象
/*
* Math 对象
* 不需要实例化 直接使用
* 记方法
* */
/*console.log(Math);*/
//绝对值
console.log(Math.abs(-1));
console.log(Math.PI);
//console.log(Math.cos());
//console.log(Math.sin(Math.PI*2));
// console.log(Math.tan(0));
Math.floor(0.9)//向下取整
Math.ceil(0.9) //向上取整
Math.round(4.4) //四舍五入
Math.max(1, 2) //判断最大值的
Math.min(1, 2) //取最小值的
Math.random()
/*随机数 0-1 取小不取大*/
Math.sqrt(4) //开平方
console.log(Math.pow(8, 1 / 3)); //求幂的
console.log(Math);
// sin 之类的值 计算得换算为弧度 角度*Math.PI/180
console.log(Math.sin(180 * Math.PI / 180));
//6.123233995736766e-17 无限接近0
console.log(Math.cos(180 * Math.PI / 180));
//eg:验证码