Reputation: 383
I am working on project which need to show some delay time in the format of "hh:mm:ss". When the seconds value reached to 60(means minutes value is 1), then the seconds value again starts from zero. But, when the minutes value reached to 60(means hours value is 1), then the minutes is showing the conitnuous value(61,62,63.....). I need to show the minutes value starts from zero when the hours value is greater than zero. How can I acheive this? Please help me and thanks in advance. Below the code which I have written.
var timer = 0;
setInterval(function () {
var hours = parseInt(timer / 3600, 10);
var minutes = parseInt(timer / 60, 10);
var seconds = parseInt(timer % 60, 10);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
$scope.delay = $scope.delayTime.label + ": " + hours + ':' + minutes + ':' + seconds;
if (++timer < 0) {
$scope.delayTime = false;
}
}, 1000);
Upvotes: 1
Views: 634
Reputation: 1512
Have you tried modulo? Like
59 % 60
-> 5960 % 60
-> 061 % 60
-> 162 % 60
-> 2Just add minutes = minutes % 60
.
Upvotes: 2
Reputation: 5397
Here's something I want to add to rasmeister's answer:
Since many browsers (especially mobile browsers) pause javascript when the browser tab is inactive, I recommend you to use Date
objects:
var start = +new Date(); // casting Date to number results in timestamp
setInterval(function () {
// get elapsed time in seconds
var timer = Math.round((+new Date() - start) / 1000);
var hours = parseInt(timer / 3600, 10);
var minutes = parseInt((timer / 60) % 60, 10);
var seconds = parseInt(timer % 60, 10);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
//$scope.delay = $scope.delayTime.label + ": " + hours + ':' + minutes + ':' + seconds;
console.log('hours ' + hours + ' minutes ' + minutes + ' seconds ' + seconds);
if (timer < 0) {
//$scope.delayTime = false;
}
}, 1000);
Upvotes: 0
Reputation: 18279
Actually, your minutes
variable contains your whole time in minutes (containing hours):
var minutes = parseInt((timer / 60) % 60, 10);
Upvotes: 1
Reputation: 1996
Just change your calc on minutes to (timer / 60) % 60
:
var timer = 3600 - 5;
setInterval(function () {
var hours = parseInt(timer / 3600, 10);
var minutes = parseInt((timer / 60) % 60, 10);
var seconds = parseInt(timer % 60, 10);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
//$scope.delay = $scope.delayTime.label + ": " + hours + ':' + minutes + ':' + seconds;
console.log('hours ' + hours + ' minutes ' + minutes + ' seconds ' + seconds);
if (++timer < 0) {
//$scope.delayTime = false;
}
}, 1000);
Upvotes: 2
Reputation:
Subtract the minutes already contained in the hours to properly calculate the minutes:
var hours = parseInt(timer / 3600, 10);
var minutes = parseInt(timer / 60 - hours * 60, 10);
var seconds = parseInt(timer % 60, 10);
Upvotes: 1