Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  121] [ 7]  / answers: 1 / hits: 42724  / 11 Years ago, thu, january 9, 2014, 12:00:00

I have several $timeout expressions in Modal controller



App.controller('ModalCtrl', function ($scope, $timeout) {
for (var i = 0; i < 10; i++) {
(function () {
var timer = $timeout(function () {
console.log('timer')
}, 1000);
})()
}
})


I need to clear all the timers when invoking the modal:



App.controller('MainCtrl', function ($scope, $modal, $timeout) {
$scope.showMap = function () {
var modal = $modal.open({
templateUrl: 'modalap.html',
controller: 'modalCtrl',
})

modal.result.then(function () { //fires when modal is resolving
}, function () { //fires when modal is invoking
});
} })


How can I do that?



PS Sorry for bad code formatting. I don't know why but I cant format it better. I duplicated code here:


More From » angularjs

 Answers
4

The $timeout service returns a Promise object which can be used to cancel the timeout.



// Start a timeout
var promise = $timeout(function() {}, 1000);

// Stop the pending timeout
$timeout.cancel(promise);


To cancel all pending timeouts, you need to maintain a list of promises and cancel the complete list when you open the modal.


[#73290] Wednesday, January 8, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yosefleod

Total Points: 113
Total Questions: 100
Total Answers: 115

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
;