使用$超时停止的事件序列的执行 [英] Stop execution of a sequence of events using $timeout
本文介绍了使用$超时停止的事件序列的执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的<一个href=\"http://stackoverflow.com/questions/23125497/how-to-execute-a-sequence-of-events-in-angular-using-timeout\">this溶液来启动序列。但现在我想停止对NG-点击。
click事件如果触发OK,我已确认对的console.log。我曾尝试
I am using this solution to start the sequence. But now I want to stop on a ng-click. The click event if firing OK, I have confirmed on the console.log. I have tried
$timeout.cancel($scope.StartChange);
但没有成功,类仍在改变。
But with no success, classes are still changing.
推荐答案
这工作,但我不知道,如果它是最好的解决办法:的 http://plnkr.co/edit/xKBHdFhXy93NqtpVooXe?p=$p$pview
This works, but I have no idea if it's the best solution: http://plnkr.co/edit/xKBHdFhXy93NqtpVooXe?p=preview
var savePromise = function(promiseObj) {
//saves promise and returns it
$scope.terminateEarly.push(promiseObj);
return promiseObj;
};
$scope.startChange = function() {
console.log('start');
$scope.state = 'b';
console.log($scope.state);
return savePromise($timeout(angular.noop, 3 * 1000))
.then(function() {
$scope.state = 'c';
console.log($scope.state);
return savePromise($timeout(angular.noop, 6 * 1000))})
.then(function() {
$scope.state = 'd';
console.log($scope.state);
return savePromise($timeout(angular.noop, 12 * 1000))})
.then(function() {
$scope.state = 'e';
console.log($scope.state);
});
};
$scope.stopChange = function(tId) {
console.log('stop');
$scope.terminateEarly.forEach(function(t){
$timeout.cancel(t);
});
};
这篇关于使用$超时停止的事件序列的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文