Date Object Date ()
-
In the computer era: 1970.01.01
-
Each object created out of date records to that moment of time
-
getTime () can be calculated time interval (ms)
var start = new Date().getTime();
for (var i = 0; i < 100000000; i ++>) {}
var end = new Date().getTime();
console.log(end - start);
-
For example scenarios
-
Limit spike shopping
-
Alarm Clock
-
Any need to obtain the time difference scene
-
Timer
-
The method of the global object window setInterval (), setTimeout (), clearInterval (), clearTimeout () is, inside this point window function
-
setInterval and setTimeout first frame is not executed
-
Once the timer into effect until canceled timer, Timeout._idleTimeout (ie, the time interval callback function is executed) can not be changed dynamically
var time = 1000;
timer = setInterval(()=> {
console.log(1);
}, time);
time = 2000;
// 依然每隔一秒打印一次1
- In fact, the timer is not precise in terms of timing
var start = new Date().getTime();
setInterval(()=>{
var end = new Date().getTime();
console.log(end - start);
start = end;
}, 1000);
-
The Executive, print the results in 1000 or 1000, is not accurate
-
Queue considerations executed from js, every 1000ms just a callback function into the execution queue, and can not guarantee timely implementation
-
Timer data structure based on a red-black tree, so there is less than 1000ms callback function is executed
-
Retrieved from "high-performance JavaScript" && "you do not know JS"
-
setTimeout scenarios distance
- Look at VIP movie five minutes? ! !