使用 $timeout 停止执行一系列事件 [英] Stop execution of a sequence of events using $timeout
本文介绍了使用 $timeout 停止执行一系列事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这个 开始序列的解决方案.但现在我想停下来单击一下.点击事件如果触发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=preview
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);
});
};
这篇关于使用 $timeout 停止执行一系列事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文