setInterval更新的数据不能够及时的在页面做更新显示,但angular自带的$interval可以做到,用法和setInterval完全一样,这里只做了秒倒计时,没有做分钟倒计时,另外timer对象为全局变量,在点击按钮和切换路由的时候需要控制清楚timer,因为angular单页面web应用的特性。
leftTimer = $interval(function(){
each.leftMsec = each.leftMsec-10;
var time = (each.leftMsec).toString();
each.Sec = time.substring(time.length-5,time.length-3);
each.MSec = time.substring(time.length-3,time.length-1);
console.log(each.Sec);
//console.log(each.MSec);
if(each.Sec.length==1){
each.Sec = "0" + each.Sec;
}else if(each.Sec==""){
each.Sec = "00";
}
if(each.MSec.length==1){
each.MSec = "0" + each.MSec;
}else if(each.MSec==""){
each.MSec = "00";
}
each.leftMsecS = ("00:"+each.Sec+":"+each.MSec)
if(each.leftMsec<=0){
each.leftMsecS = '正在揭晓......';
$interval.cancel(leftTimer);
}
},10);