admin管理员组文章数量:1399474
Hello i want to create a simple script to run a countdown who start at 05:00 and finish at 00:00. I need help please. I can not find a script that works. Ive tested with function new Date() but i get a format like this Days/Hours/Minutes/Seconds and me i need only Minutes/Seconds. When the countdown is finished an alert appears with a message and the countdown is stopped at 00:00.
For the moment I've this:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.carname = "Volvo";
$scope.timer = timer();
function timer() {
var minutes = "03";
var secondes = "00";
var init = minutes + ":" + secondes;
var res = document.getElementById("timer").innerHTML = init;
return res;
}
});
<script src=".6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<h1>{{carname}}</h1>
<h4 id="timer">{{timer()}}</h4>
</div>
Hello i want to create a simple script to run a countdown who start at 05:00 and finish at 00:00. I need help please. I can not find a script that works. Ive tested with function new Date() but i get a format like this Days/Hours/Minutes/Seconds and me i need only Minutes/Seconds. When the countdown is finished an alert appears with a message and the countdown is stopped at 00:00.
For the moment I've this:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.carname = "Volvo";
$scope.timer = timer();
function timer() {
var minutes = "03";
var secondes = "00";
var init = minutes + ":" + secondes;
var res = document.getElementById("timer").innerHTML = init;
return res;
}
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<h1>{{carname}}</h1>
<h4 id="timer">{{timer()}}</h4>
</div>
Share
Improve this question
asked Oct 26, 2017 at 5:56
salimbenfarhatsalimbenfarhat
651 silver badge9 bronze badges
1
- did the answer help? – Sajeetharan Commented Oct 27, 2017 at 4:58
3 Answers
Reset to default 8you can simply use $interval
function and use a custom filter
to display it in seconds
$interval(function(){console.log($scope.counter--)},1000);
DEMO
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {
$scope.carname = "Volvo";
$scope.counter = 180;
$interval(function(){console.log($scope.counter--)},1000);
});
app.filter('counter', [function() {
return function(seconds) {
return new Date(1970, 0, 1).setSeconds(seconds);
};
}])
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<h1>{{carname}}</h1>
{{counter | counter | date:'mm:ss'}}
</div>
I took an example from here, slightly modified and here it is:
<!DOCTYPE HTML>
<html>
<head>
<style>
p {
text-align: center;
font-size: 60px;
}
</style>
</head>
<body>
<p id="demo"></p>
<script>
// Set the date we're counting down to
var countDownDate = new Date().setMinutes(new Date().getMinutes()+5);
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
</body>
</html>
Here is a simple example about countdown function using 'let' variable:
function countDown(time) {
for(let i = time; i >= 0; i--){
setTimeout(function () {
console.log(i);
},(time - i) * 1000);
}
}
countDown(5);
Here is a simple example about countdown recursion function using 'var' variable and:
function countDown2(time){
for(var i = time; i >= 0; i--){
setTimeout(function () {
console.log(time);
time--;
},(time-i)*1000);
}
}
countDown2(5);
本文标签: coundown minutes in angularjs or javascriptStack Overflow
版权声明:本文标题:coundown minutes in angularjs or javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744128505a2592067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论