时间倒计时(针对于秒杀等前台判断)
<body ng-app="wechat" ng-controller="wechatCtrl" ng-init="loadCart()">
<div>
您的支付时间剩余:<span style="color: red">{{timeString}}</span><br>
</div>
<script>
var app = angular.module("wechat",[]);
app.controller("wechatCtrl",function ($http,$scope,$location,$interval) {
$scope.loadCart = function () {
var endTime = "2019-12-10 14:45:12";
endTime = new Date(endTime).getTime();
var nowDate = new Date().getTime();
var second =Math.floor(endTime-nowDate)/1000
second = parseInt(second)
time= $interval(function(){
if(second>0){
second =second-1;
$scope.timeString=convertTimeString(second);
}else{
$interval.cancel(time);
alert("秒杀服务已结束");
}
},1000);
convertTimeString=function(allsecond){
console.log(allsecond)
var days= Math.floor( allsecond/(60*60*24));
var hours= Math.floor( (allsecond-days*60*60*24)/(60*60) );
var minutes= Math.floor( (allsecond -days*60*60*24 - hours*60*60)/60);
var seconds= allsecond -days*60*60*24 - hours*60*60 -minutes*60;
var timeString="";
if(days>0){
timeString=days+"天 ";
}
return timeString+hours+"小时"+minutes+"分钟"+seconds+"秒";
}
}
</script>
</body>
</html>