This article mainly introduces the pitfalls encountered by AngualrJs clearing timer. Friends in need can refer to
angualrJs clearing timer climbing pitfalls:
I found a strange problem today , the timer placed in the custom instruction is still executing on another page after the page jumps. This is definitely not possible and will affect the performance of the system.
I used the native method window.onunload in Angular but it didn’t work, so I had to use Angular’s own method $destroy. This page jumps and the DOM structure changes, the timer can be cleared.
var timer = setInterval(function(){ $scope.$apply(function(){ //这里是想要定时刷新的逻辑 }); },3000); $scope.$on('$destroy',function(){ if (timer) { clearInterval(timer); timer = null; } });
Let me talk here, because I use the native setTimeout()
and setInterval()
functions in javascript, so when clearing The corresponding are clearTimeout()
and clearInterval()
, the angular timer is $timeOut
and $interval
, so the clear corresponding is $timeOut.cancel()
and $interval.cancel()
,
must correspond one to one. Inconsistencies will not be cleared.
Summarize
The above is the detailed content of Problems encountered when clearing timers in AngualrJs. For more information, please follow other related articles on the PHP Chinese website!