Home > Web Front-end > JS Tutorial > body text

How AngualrJs solves the pitfalls encountered in clearing timers

小云云
Release: 2018-01-29 10:55:27
Original
1296 people have browsed it

This article mainly introduces the pitfalls encountered by AngualrJs clearing timers. Friends who need it can refer to it. I hope it can help everyone.

I discovered 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 system. performance.

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;
          }
        });
Copy after login

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.

Related recommendations:

Javascript timer implements progress bar function

The above is the detailed content of How AngualrJs solves the pitfalls encountered in clearing timers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!