我正在使用this解决方案来启动序列。但现在我想停止点击。 单击事件,如果激活OK,我已在console.log上确认。我试过了
$timeout.cancel($scope.StartChange);
但是没有成功,课程仍在改变。
答案 0 :(得分:1)
这有效,但我不知道它是否是最佳解决方案: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);
});
};