当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS Math对象、日期对象、函数、定时器

JS Math对象、日期对象、函数、定时器

2019年12月14日  | 移动技术网IT编程  | 我要评论

math对象

  • 开平方:sqrt
  • 绝对值:abs
  • π:pi
  • x的y次方:pow
  • 四舍五入取整:round
  • 向下取整:floor
  • 向上取整:ceil
  • 最大值:max
  • 最小值: min
  • 随机数:random
var br = "<br>";
document.write(math.sqrt(9) + br);//开平方
document.write(math.abs(-9) + br);//绝对值
document.write(math.pi + br);//π:3.141592653589793....
document.write(math.pow(2, 10) + br);//x的y次方
document.write(math.round(3.5) + br);//四舍五入取整
document.write(math.floor(3.9) + br);//向下取整
document.write(math.ceil(3.1) + br);//向上取整
document.write(math.max(8, 2, 4, 21) + br);//最大值
document.write(math.min(8, 2, 4, 21) + br);//最小值
document.write(math.random() * 100 + br);//随机数:0-1


日期对象

  • 获取当前时间:date()
  • 获取年:getfullyear
  • 获取月:getmonth
  • 获取日:getdate
  • 获取周几:getday
  • 获取时:gethours
  • 获取分:getminutes
  • 获取秒:getseconds
  • 时间戳:date.now()
var br = "<br>";
var datetime = new date();
document.write(date() + br);//获取当前时间
document.write(datetime.getfullyear() + br);//获取年
document.write(datetime.getmonth() + 1 + br);//获取月(0-11)
document.write(datetime.getdate() + br);//获取日
document.write(datetime.getday() + br);//获取周几
document.write(datetime.gethours() + br);//获取时
document.write(datetime.getminutes() + br);//获取分
document.write(datetime.getseconds() + br);//获取秒
document.write(date.now() + br);//时间戳


函数

  • 定义函数:function funname(){}
  • 函数分类
    • 有名函数
    //有名函数
    //不定参
    function func() {
      return arguments[2] * arguments[4]
    }
    document.write(func(0, 1, 2, 3, 4));
    • 匿名函数
    // 匿名函数一般充当事件函数
    var box = document.getelementbyid("box");
    box.onclick = function () {
      alert("===")
    }
  • 作用域
    • 加var定义,子作用域不会修改父作用域的值
    var num = 111;
    function eject() {
      var num = 999;
      alert(num)//999
    }
    alert(num);//111
    eject();
    alert(num);//111
    • 不加var定义,子作用域会修改父作用域的值
    var num = 111;
    function eject() {
      num = 999;
      alert(num)//999
    }
    alert(num);//111
    eject();
    alert(num);//999

定时器

  • 设置定时器:settimeout(只执行一次)
  • 清除定时器:cleartimeout
  • 设置定时器:setinterval(一直执行)
  • 清除定时器:clearinterval
function log() {
    console.log("---")
}
//只执行一次
settimeout(log, 1000);
//一直执行
var timer = setinterval("log()",1000);

var btn = document.getelementsbytagname("button")[0];
btn.onclick = function () {
    //清除定时器
    clearinterval(timer);
}




如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网