简体   繁体   English

我如何修改我的功能以能够取消控制器中任何位置的间隔

[英]How can I modify my function to be able to cancel the interval from anywhere in the controller

I am trying to create a countdown and I want to be able to cancel the interval not only when reach 0 but with clicking of a button as well.How can I modify my function to be able to cancel the interval from anywhere in the controller. 我正在尝试创建一个倒数计时器,我不仅希望能够在达到0时取消间隔,而且还可以单击按钮来取消间隔。如何修改我的功能以能够从控制器中的任何位置取消间隔。

function countDown(total) {
total = total * 60;
var interval = $interval(function () {
    var minutes = Math.floor(total / 60);
    var seconds = total - minutes * 60;
    if (minutes < 1)
    {
        minutes = '0:';
    }
    else
    {
        minutes = minutes + ':';
    }

    if (seconds < 10)
    {
        seconds = '0' + seconds;
    }

    self.remainingTime = "Remaining time: " + minutes + seconds;
    total--;

    if(minutes === '0:' && seconds === '00')
    {
        $interval.cancel(interval);
    }
}, 1000);

} }

As suggested by other users, you should make interval global in the controller. 如其他用户所建议,您应该在控制器中将interval全局。 Then on the other functions you want to cancel the interval you can call it safely. 然后,在其他要取消interval功能上,可以安全地调用它。

Take a look at the below sample: 看下面的示例:

 angular.module('app', []) .controller('ctrl', function($interval) { var self = this; var interval; self.wizard = { startInterval: startInterval, cancelInterval: cancelInterval }; startInterval(); return self.wizard; function startInterval() { countDown(24); } function cancelInterval() { $interval.cancel(interval); } function countDown(total) { cancelInterval(); total = total * 60; interval = $interval(function() { var minutes = Math.floor(total / 60); var seconds = total - minutes * 60; if (minutes < 1) { minutes = '0:'; } else { minutes = minutes + ':'; } if (seconds < 10) { seconds = '0' + seconds; } self.wizard.remainingTime = "Remaining time: " + minutes + seconds; total--; if (minutes === '0:' && seconds === '00') { $interval.cancel(interval); } }, 1000); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <span ng-app="app" ng-controller="ctrl as ctrl"> <span ng-bind="ctrl.remainingTime"> </span> <br/> <a href="" ng-click="ctrl.cancelInterval()"> Cancel Interval </a> <br/> <a href="" ng-click="ctrl.startInterval()"> Start Interval </a> </span> 

I created an example using self instead of $scope like you are in your snippet above. 我创建了一个使用self而不是$ scope的示例,就像您在上面的代码段中一样。 As others mentioned moving the interval var to the global scope fixes this. 正如其他人提到的,将间隔var移到全局范围可以解决此问题。 I also added a stop and start function to test the issue you mentioned in the example above. 我还添加了停止和启动功能,以测试您在以上示例中提到的问题。

UPDATED - I added some code to the stop interval to reset the interval to undefined. 更新-我向停止间隔添加了一些代码以将间隔重置为未定义。 I found an example of this usage on Angular's site so it might be worth a try to see if it fixes your other issue. 我在Angular网站上找到了这种用法的示例,因此值得一试,看看它是否可以解决您的其他问题。

var app = angular.module('app', []);

app.controller('BaseController', function($interval) {
  var self = this;
  var interval;

  function countDown(total) {
    total = total * 60;
    interval = $interval(function() {
      var minutes = Math.floor(total / 60);
      var seconds = total - minutes * 60;
      if (minutes < 1) {
        minutes = '0:';
      } else {
        minutes = minutes + ':';
      }

      if (seconds < 10) {
        seconds = '0' + seconds;
      }

      self.remainingTime = "Remaining time: " + minutes + seconds;
      total--;

      if (minutes === '0:' && seconds === '00') {
        $interval.cancel(interval);
      }
    }, 1000);
  }

  self.stopInterval = function(){
    if (angular.isDefined(interval)) {
      $interval.cancel(interval);
      interval = undefined;
    }
  }

  self.startInterval = function(){
    countDown(10);
  }
});

Then in the html I added a button to start and stop the interval like so: 然后在html中,我添加了一个按钮来启动和停止间隔,如下所示:

<body ng-app="app">
  <div ng-controller="BaseController as baseCtrl">
    {{ baseCtrl.remainingTime }}
    <div>
      <button ng-click="baseCtrl.startInterval()">Start Countdown</button>
      <button ng-click="baseCtrl.stopInterval()">Stop Countdown</button>
    </div>
  </div>
</body>

You can view the codepen example here 您可以在此处查看codepen示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何取消javascript中间隔内的函数调用? - How can I cancel a function call that's inside an interval in javascript? 如何修改jQuery函数以更新数字计数器 - how can I modify my jQuery function to update the number counter 如何使用间隔循环修改HTML Canvas颜色? - How can I modify HTML Canvas colors using an interval loop? 如何在连接回调之外的任何位置从应用程序中发出套接字? - How can I emit sockets from anywhere in my app outside the callback of the connection? 如何取消JS函数? - How can I cancel a JS function? 如果我的函数评估为true,如何使我的Confirm语句过帐状态;如果为false,如何取消? - How can I make my confirm statement post a status if my function evaluates as true, or cancel if false? 如何从主干控制器执行 angularJS 控制器中的函数 - How can I execute a function in an angularJS controller from a backbone controller 如何根据指令中控制器的值修改指令的templateURL? - How can I modify my directive's templateURL based on values within its controller? 我可以在节点 js 中修改来自 mysql 服务器的响应吗? - can i able to modify response from mysql server in node js? 从运行块我可以在另一个控制器而不是$ rootscope内调用角度范围函数 - From run block can i able to call angular scope function inside another controller instead of $rootscope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM