【问题标题】:coundown minutes in angularjs or javascriptangularjs或javascript中的倒计时分钟
【发布时间】:2017-10-26 05:56:57
【问题描述】:

您好,我想创建一个简单的脚本来运行从 05:00 开始并在 00:00 结束的倒计时。我需要帮助。我找不到有效的脚本。我已经用函数 new Date() 进行了测试,但我得到了这样的格式天/小时/分钟/秒,而我只需要分钟/秒。倒计时结束后,会出现一条带有消息的警报,并且倒计时在 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>

【问题讨论】:

  • 答案有帮助吗?

标签: javascript angularjs countdown


【解决方案1】:

您可以简单地使用 $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>

【讨论】:

  • 很好,谢谢。现在我如何在倒计时结束(0:00)时添加一个停止?
【解决方案2】:

我从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>

【讨论】:

    【解决方案3】:

    这是一个关于使用'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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-15
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      相关资源
      最近更新 更多