Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

docs(interval.js) alerting the developer #5377

Closed
wants to merge 1 commit into from

Conversation

joshkurz
Copy link
Contributor

Many times $intervals are used inside of directives or controllers. If a directive/controller is destroyed, then the $interval should be destroyed as well. The interval created by this service does not have a way to track an associated $scope. This could cause some issues with developers who assume that the $interval will be cleared for them when the $scope is destroyed.

Many times $intervals are used inside of directives or controllers. If a directive/controller is destroyed, then the $interval should be destroyed as well. The interval created by this service does not have a way to track an associated $scope. This could cause some issues with developers who assume that the $interval will be cleared for them when the $scope is destroyed.

I believe that the library could/should handle this as well, but thats another issue.
@joshkurz
Copy link
Contributor Author

Does it make sense to allow for an optional $scope reference parameter to the $interval's constructor?
This way when the interval is running it could check if its $scope has been destroyed.

Or maybe there is a better way to monitor the specific context the $interval lives in.

or is it just good practice to always do this?

 $scope.$on('$destroy', function(x){
    $interval.cancel(intervalRef);
 });

“For setInterval, the completeness occurs on clearInterval. That may lead to memory leaks when the function actually does nothing, but the interval is not cleared.“ (http://javascript.info/tutorial/memory-leaks#setinterval-settimeout)

@petebacondarwin
Copy link
Member

I agree that we need to highlight this to developers but coupling it to the scope is not ideal, since there are many times when you create an interval elsewhere. We just have to encourage them to manage it explicitly.

jamesdaily pushed a commit to jamesdaily/angular.js that referenced this pull request Jan 27, 2014
It is essential that users of `$interval` destroy the interval when they are finished.
Otherwise you can get memory leaks.
Often `$intervals` are used in directives or controllers and developers don't think
about what happens when the component is destroyed.
If a directive/controller scope is destroyed, then the $interval should be destroyed as well.
This could cause some issues with developers who assume that the interval will be cleared
for them when the scope is destroyed.

Closes angular#5377

I believe that the library could/should handle this as well, but thats another issue.
jamesdaily pushed a commit to jamesdaily/angular.js that referenced this pull request Jan 27, 2014
It is essential that users of `$interval` destroy the interval when they are finished.
Otherwise you can get memory leaks.
Often `$intervals` are used in directives or controllers and developers don't think
about what happens when the component is destroyed.
If a directive/controller scope is destroyed, then the $interval should be destroyed as well.
This could cause some issues with developers who assume that the interval will be cleared
for them when the scope is destroyed.

Closes angular#5377

I believe that the library could/should handle this as well, but thats another issue.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants