1. 程式人生 > >js筆記:數學物件+日期+定時器

js筆記:數學物件+日期+定時器

1、數學物件 Math   特點 :不需要定義  直接通過 Math. 呼叫方法     Math.pow(m,n) m的n次冪     Math.sqrt(m) 平方根     勾股定理 : c = Math.sqrt(Math.pow(a,2) + Math.pow(b,2))     Math.abs(m) 絕對值     Math.floor(m)  向下取整  (小於m的最大整數)     Math.ceil(m) 向上取整   (大於m的最小整數)
    Math.round() 四捨五入      Math.max() 獲取最大值     Math.min() 獲取最小值     Math.random()  [0,1)  獲取0--1之間的隨機小數   如何利用隨機數獲取任意區間整數值   獲取任意區間值整數的函式:     function rand(min,max){         return Math.round( Math.random()*(max-min) + min );     } 2、日期時間物件 Date

  getFullYear() 年
  getMonth() 月 d.getMonth()+1 月份從0開始
  getDate() 日期 從 Date 物件返回一週中的某一天 (0 ~ 6)。
  getHours() 返回 Date 物件的小時 (0 ~ 23)。
  getMinutes() 返回 Date 物件的分鐘 (0 ~ 59)。
  getSeconds() 返回 Date 物件的秒數 (0 ~ 59)。
  getTime() 返回 1970 年 1 月 1 日至今的毫秒數。
  Date.parse() 返回1970年1月1日午夜到指定日期(字串)的毫秒數。
  setDate() 設定 Date 物件中月的某一天 (1 ~ 31)。
  setMonth() 設定 Date 物件中月份 (0 ~ 11)。
  setFullYear() 設定 Date 物件中的年份(四位數字)。
  toLocaleString() 年月日時分秒
  toLocaleDateString() 年月日

  var time = new Date(); //不傳參 是當前時間 Thu Jan 03 2019 14:09:27 GMT+0800 (中國標準時間)

            //var time1 = new Date("2019/3/1"); //傳參 代表你傳的時間   轉為  中國標準時間

3、定時器

  單位:毫秒
  定時器分為:

    單次定時
      setTimeout(function(){
      console.log(1);
      },時間)
    多次定時
      setInterval(function(){
      
      },時間)
  如果想移除定時器,就要給定時器起個名字
    clearInterval(定時器的名字);
    clearTimeout(定時器的名字);