I have done some javascript coding in my views controller.
The first time I visit the route it works as it's suppose to, but when I go to another route and go back to it, the javascript seems to be executing twice and the effect that I've done is messed up (doubled).
Here's the code that I have:
'use strict'
angular.module('myApp')
.controller('FuncionesCtrl', function($scope) {
$scope.viewName = 'Funciones';
var count = 0;
var array = [
'identificada',
'en espera',
'entre tres'
];
$('#teasers').html(array[0]);
setInterval(function (){
if(count == 2) {
console.log('if');
count = 0;
$('#teasers').slideUp(500);
setTimeout(function (){
$('#teasers').html(array[count]);
},500);
$('#teasers').slideDown(500);
} else {
console.log('else');
count ++;
$('#teasers').slideUp(500);
setTimeout(function (){
$('#teasers').html(array[count]);
},500);
$('#teasers').slideDown(500);
}
}, 2000);
});
This is an animation where a string slides up (disappears) and then slides down as a different word. The first time it works nice, but after I visit the route again, the animation seems to speed up.
How can I fix this?
Thanks!
edit
I have done some changes:
'use strict'
angular.module('myApp')
.controller('FuncionesCtrl', function($scope, $interval) {
$scope.viewName = 'Funciones';
clearInterval(interval);
var count = 0;
var array = [
'identificada',
'en espera',
'entre tres'
];
$('#teasers').html(array[0]);
var interval = setInterval(function (){
if(count == 2) {
console.log('if');
count = 0;
$('#teasers').slideUp(500);
setTimeout(function (){
$('#teasers').html(array[count]);
},500);
$('#teasers').slideDown(500);
} else {
console.log('else');
count ++;
$('#teasers').slideUp(500);
setTimeout(function (){
$('#teasers').html(array[count]);
},500);
$('#teasers').slideDown(500);
}
}, 2000);
});
the clearInterval
doesn't seem to do anything.
solution
I've manage to figure it out:
'use strict'
angular.module('myApp')
.controller('FuncionesCtrl', function($scope, $interval) {
$scope.viewName = 'Funciones';
// clearInterval(interval);
var count = 0;
var array = [
'identificada',
'en espera',
'entre tres'
];
$('#teasers').html(array[0]);
var interval = $interval(function (){
if(count == 2) {
console.log('if');
count = 0;
$('#teasers').slideUp(500);
setTimeout(function (){
$('#teasers').html(array[count]);
},500);
$('#teasers').slideDown(500);
} else {
console.log('else');
count ++;
$('#teasers').slideUp(500);
setTimeout(function (){
$('#teasers').html(array[count]);
},500);
$('#teasers').slideDown(500);
}
}, 2000);
$scope.$on('$destroy', function (e){
$interval.cancel(interval);
});
});
This:
$scope.$on('$destroy', function (e){
$interval.cancel(interval);
});
does the trick.
Reference: https://stackoverflow.com/a/14238039/3632722