angularjs 或 javascript 倒计时分钟

coundown minutes in angularjs or javascript

你好,我想创建一个简单的脚本来 运行 从 05:00 开始并在 00:00 结束的倒计时。我需要帮助。我找不到有效的脚本。我用函数 new Date() 进行了测试,但我得到了这样的格式 Days/Hours/Minutes/Seconds 而我只需要 Minutes/Seconds。倒计时结束后,会出现一条带有消息的警报,倒计时在 00:00 处停止。

目前我有这个:

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.com/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>

我从 here 中拿了一个例子,稍作修改,这里是:

<!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>

你可以简单地使用$interval函数并使用custom filter来秒显示它

$interval(function(){console.log($scope.counter--)},1000);

演示

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.com/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>

这是一个使用 'let' 变量的倒计时函数的简单示例:

function countDown(time) {
  for(let i  = time; i >= 0; i--){
    setTimeout(function () {
      console.log(i);
    },(time - i) * 1000);
  }
}
countDown(5);

这是一个使用 'var' 变量和:

的倒计时递归函数的简单示例
function countDown2(time){
  for(var i  = time; i >= 0; i--){
      setTimeout(function () {
        console.log(time);
        time--;
      },(time-i)*1000);
    }
}
countDown2(5);