[英]how to pause and resume the timer when using $timeout in angularjs
i used this code for pause timer:我将此代码用于暂停计时器:
when using $timeout
in angularjs how to implement pause and resume functionality在 angularjs 中使用$timeout
时如何实现暂停和恢复功能
$scope.pause = function(){
window.clearTimeout(t);
}
timedcounter = function () {
time = time + 1;
localStorage.autotimertime = time;
t = $timeout(timedcounter, 1000);
display(time);
}
In this snippet, I am showing the time and a button is used to stop/restart the timeout.在这个片段中,我显示了时间,一个按钮用于停止/重新启动超时。
You should add your logic inside the onTimeout
function您应该在onTimeout
函数中添加您的逻辑
var app = angular.module('myapp', []); app.controller('myCtrl', function($scope, $timeout) { $scope.time = 0; $scope.stopped = false; $scope.onTimeout = function() { $scope.time = $scope.time + 1; //Your logic here //localStorage.autotimertime = time; //display(time); mytimeout = $timeout($scope.onTimeout, 1000); } var mytimeout = $timeout($scope.onTimeout, 1000); $scope.stopTimeout = function() { $scope.stopped = true; $timeout.cancel(mytimeout); } $scope.restatTimeout = function() { $scope.stopped = false; mytimeout = $timeout($scope.onTimeout, 1000); } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myapp" ng-controller="myCtrl"> <p ng-bind="time"></p> <button ng-click="stopTimeout()" ng-show="!stopped">STOP ME !</button> <button ng-click="restatTimeout()" ng-show="stopped">RESUME ME!</button> </div>
要取消超时请求,请调用 $timeout.cancel(promise)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.