Home >Web Front-end >JS Tutorial >Using timers with AngualrJs

Using timers with AngualrJs

php中世界最好的语言
php中世界最好的语言Original
2018-04-17 15:55:261504browse

This time I will bring you the use of timers with AngualrJs, what are the precautions for using timers with AngualrJs, the following is a practical case, let’s take a look .

angualrJs clear timer climbing pit road:

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;
          }
        });
Let me talk about it here, because I am using the

setTimeout()

and setInterval() functions native to javascript, so the corresponding They are clearTimeout() and clearInterval(), and the angular timer is $timeOut and $interval, so the clear corresponding is $timeOut.cancel()and$interval.cancel(), There must be a one-to-one correspondence, and inconsistencies will not be cleared.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

JS implements random number generation for deduplication


How to use exports and module.exports



The above is the detailed content of Using timers with AngualrJs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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