<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body ng-app="wechat" ng-controller="wechatCtrl" ng-init="loadCart()">
<div>
您的支付时间剩余:<span style="color: red">{{timeString}}</span><br>
</div>
<script src="https://code.angularjs.org/1.7.9/angular.min.js"></script>
<script>
var app = angular.module("wechat", []);
app.controller("wechatCtrl", function($http, $scope, $location, $interval) {
$scope.loadCart = function() {
//定义一个订单结束时间
var endTime = "2019-12-12 00:00:00";
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;
//转换时间字符串 调用下面的方法 convertTimeString()
$scope.timeString = convertTimeString(second);
} else {
$interval.cancel(time);
alert("秒杀服务已结束");
}
}, 1000);
//转换秒为 天小时分钟秒格式 XXX天 10:22:33
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>
时间倒计时(针对于秒杀等前台判断)
最新推荐文章于 2021-10-28 17:07:02 发布