日期对象,是系统提供好的,并且是静态的拍照片
时间戳
var firstTime = new Date().getTime(); for(var i=0;i<10000000;i++){} var lastTime = new Date().getTime(); console(lastTime - firstTime)
var date = new Date(); date.setMinutes(16); setInterval(function(){ if(new Date().getTime() - date.getTime() > 1000){ console.log("start") } },1000)
var time = 1000; setInterval(function(){ console.log("a"); },time) time = 2000 //上面setInterval里的time为1000不会变,只取一次
看setInterval执行的时间和系统时间是否有误差
var firstTime = new Date().getTime(); setInterval(function(){ var lastTime = new Date().getTime(); console.log(lastTime - firstTime); firstTime = lastTime; },1000)
setInterval("console.log('a');",1000)
倒计时
var minutesNode = document.getElementsByTagName('input')[0]; var secondsNode = document.getElementsByTagName('input')[1]; var minutes = 0, seconds = 0; var timer = setInterval(function(){ seconds++; if(seconds == 60){ seconds = 0; minutes ++ ; } secondsNode.val = seconds; minutesNode.val = minutes; if(minutes == 3){ clearInterval(timer); } })