文章目录
  1. 1. Math和日期对象
    1. 1.1. Math 对象用于执行数学任务。

Math和日期对象

Math 对象用于执行数学任务。

  • Math.PI
    返回圆周率(约等于3.14159)

    案例:
    console.log(math)
    
  • Math.abs(100);//取绝对值,去负的过程

    var num = 10.1;
    
  • console.log(Math.ceil(num));//对数值进行上舍入,有小数点时就进1

    var num = 10.1;
    
  • console.log(Math.floor(num));//对数值进行下舍入,有小数点时就舍去

    var num = 10.9;
    
  • console.log(Math.round(num));//对数值进行四舍五入

  1. max(x,y);
    返回x,y中的最高值

  2. min(x,y);
    返回x,y中的最底值

  3. random();
    返回0~1之间的随机数

consloe.log(Math.random());

  • random()用法

生成n到m之间的整数;

生成n~m,包含n但不包含m的整数;

第一步算出m-n的值,假设等于w;
第二步Math.random()w;
第三步Math.random()
w+n;
第四步parseInt(Math.random()*w+n,10)//十进制

案例:
var n=50,m=100;
var w=m-n;
console.log(parseInt(Math.random()*w+n,10));//
50~99之间随机
  • 生成n~m,不包含n但包含m的整数;

第一步算出m-n的值,假设等于w
第二步Math.random()w
第三步Math.random()
w+n
第四步Math.floor(Math.random()*w+n)+1

  • 生成n~m,不包含n和m的整数;

第一步算出m-n-2的值,假设等于w
第二步Math.random()w
第三步Math.random()
w+n+1
第四步Math.round(Math.random()w+n+1)或者Math.ceil(Math.random()w+n+1)

  1. 定时器:
    setInterval()循环定时器,间隔是毫秒1s=1000ms

setInterval()函数语法

clearInterval()清除定时

案例:
var id=setInterval(function(){},1000);
clearInterval(id);

在任何地方使用定时器,先保存它

var xx = null;
var yy = null;
var num = 0;
(function(){
    xx = setInterval(add,2000);
    function add(){
        num ++;
        if(num > 10){
            clearInterval(xx);
        }
    }
})()
  • 单次定时器:

setTimeout(‘’,3000);//3秒后执行这个方法,只运行一次

clearTimeout();//清除定时器

案例:
var id=setTimeout(function(){},1000);
clearTimeout(id);

在任何地方使用定时器,先保存它

var xx = null;
var yy = null;
var num = 0;
(function(){
    xx = setTimeout(add,2000);
    function add(){
        num ++;
        if(num > 10){
            clearTimeout(xx);
        }
    }
})()

函数都有什么?

答:1.arguments 参数对象
2.prototype 原型对象
3.默认的返回值undefined
4.call和apply
call和apply是用一个对象来替换当前对象的

案例:
function Man(){
    this.name = "huai";
    this.showName = function(){
        alert(this.name);
    }
}
function All(){
    this.name = "hao";
    // alert(this.name);
}
var man = new Man();
var all = new All();
var obj = {};
// man.showName.apply(all,[]);//调用的时候必须要用词组
方法
man.showName.call(all,"");//调用的时候不限定是什么方法
  1. 日期对象:

var time = new Date();//日期对象,时间对象,date对象

console.log(time.getFullYear());//方法

console.log(time.getYeat()+1900);//废了

console.log(time.getMonth());//0~11 0代表1月,11代表12月

console.log(time.getDate());//1~31 代表31天 获取日数

console.log(time.getDay());//0~6 0代表周日 获取周几

console.log(time.getHours());//0~23 0~11 am 12~23 pm 获取24个小时

console.log(time.getMinutes());//0~59 代表60分 获取分

console.log(time.getSeconds());//0~59 代表60秒 获取秒

案例:
var box = document.querySelector("body");

setInterval(function(){
    var time = new Date();
    console.log(time);
    box.innerHTML = time.getFullYear()+"年"+(time.getMonth()+1)+"月"+time.getDate()+"日"+shiduan(time.getHours())+buling(time.getHours())+":"+buling(time.getMinutes())+":"+buling(time.getSeconds())+"星期"+buzhou(time.getDay());
},30)
function buling(num){
    if(parseInt(num,10)<10){
        return "0"+num;
    }else{
        return num;
    }
}
function buzhou(num){
    if(num == 0){
        return "天";
    }else if(num == 1){
        return "一";
    }else if(num == 2){
        return "二";
    }else if(num == 3){
        return "三";
    }else if(num == 4){
        return "四";
    }else if(num == 5){
        return "五";
    }else if(num == 6){
        return "六";
    }
}
function shiduan(num){
    if(num<12){
        return "am";
    }else{
        return "pm"
    }
}

new Date的传参的四种方式:

第一种最常用

  1. var time = new Date(2015,11,30,23,50,50);//传参,设置时间,年月日,时分秒,毫秒
    console.log(time);
  2. var time2 = new Date(“July 23,2015,23:12:34”);
    console.log(time);
  3. var time3 = time.getTime();//1700年1月1日至今的毫秒数
    console.log(time3);
  4. var time4 = new Date(1451490650000);
    console.log(time4);

    month[0]="January";//一月 
    
    month[0]="February";//二月
    
    month[0]="March";//三月
    
    month[0]="April";//四月
    
    month[0]="May";//五月
    
    month[0]="June";//六月
    
    month[0]="July";//七月
    
    month[0]="August";//八月
    
    month[0]="September";//九月
    
    month[0]="October";//十月  
    
    month[0]="November";//十一月
    
    month[0]=“December";//十二月
    

如何设计倒计时:

请看案例!

文章目录
  1. 1. Math和日期对象
    1. 1.1. Math 对象用于执行数学任务。
Fork me on GitHub