var app = angular.module("timerApp", []);
app.controller("timerController",['$scope','$interval','$filter', function($scope, $interval, $filter){
//initalizing variables so they aren't defined for the first time in the time
// (And therefore take a second to show up)
$scope.twoDaysFromNow = Math.floor(Date.now()/1000) + (60 * 60 * 24 * 2);
$scope.goal = ($scope.twoDaysFromNow);
$scope.now = Math.floor(Date.now()/1000);
$scope.time = $scope.goal - $scope.now;
$interval(function(){
$scope.now = Math.floor(Date.now()/1000);
$scope.time = $scope.goal - $scope.now;
},1000,0,null);
}]);
app.filter('timeleft', function() {
function isInteger(x) {
return x % 1 === 0;
}
return function(value) {
if (value && isInteger(value))
return toTimeLeft(value);
return value;
};
});
function toTimeLeft(t) {
var days, hours, minutes, seconds;
//display the words "days", "Hours" etc.
var daysD = ' day', hoursD = ' hour', minutesD = ' minute', secondsD = ' second';
days = Math.floor(t/86400);
t -= days * 86400;
hours = Math.floor(t/3600) % 24;
t -= hours * 3600;
minutes = Math.floor(t/60) % 60;
t -= minutes * 60;
seconds = t % 60;
//Add an 's' on the end if it's not 1
if (seconds !== 1){secondsD += 's';}
if (minutes !== 1){minutesD += 's';}
if (hours !== 1){hoursD += 's';}
if (days !== 1){daysD += 's';}
// padding the numbers
return [
pad(days, 2) + daysD,
pad(hours, 2) + hoursD,
pad(minutes, 2) + minutesD,
pad(seconds, 2) + secondsD
].join(', ');
};
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
@import url('https://fonts.googleapis.com/css?family=Ubuntu');
html {
background: #305e8e;
color: #fff;
text-align: center;
font-family: 'Ubuntu', sans-serif;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="timerApp">
<div ng-controller="timerController">
<h1>Hurry up! Deal ends in:</h1>
<h2>{{time | timeleft}}</h2>
</div>
</div>
您的退貨聲明應退貨(小時+':'+分鐘S + ':' +秒); – V31 2014-09-11 12:06:55
謝謝@ v31。但我怎麼做只顯示2位數和倒計時 – 2014-09-11 12:15:59
爲什麼不能使用角度計時器? http://siddii.github.io/angular-timer/節省時間 – Prasad 2015-06-02 17:12:15