JavaScript侦听器不断增加 [英] JavaScript listeners keep increasing

查看:140
本文介绍了JavaScript侦听器不断增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个Web应用程序,并使用Google开发人员工具监控了性能.我注意到侦听器不断增加,堆也在增加.

I implemented a web application and monitored the performance with the google developer tools. I notice that the listeners keep increasing and so is the heap.

听众正在增加的部分看起来像这样

The part where the listeners are increasing looks something like this

let ival = $interval(function () {
    $http.get('someurl') // this call is actually done through a service, don't know if that matters
}, 1000)

我会理解堆是否由于垃圾收集器未收集到的某些数据而增长,但我不明白为什么侦听器在增加?

I would understand if the heap grows because of some data not beeing collected by the garbage collector but I don't understand why the listeners are increasing?

这是一个可重现的最小示例:

Here is a reproducable, minimal example:

index.html文件:

The index.html file:

<!doctype html>
<html ng-app="exampleModule">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
    <script src="script.js"></script>
  </head>
  <body>
    <div ng-controller="someController">
    </div>
  </body>
</html>

还有script.js文件:

And the script.js file:

angular.module("exampleModule", [])
  .controller("someController", [ "$http", "$interval", function ($http, $interval) {

    $interval(function () {
      $http.get('script.js')
    }, 1000)

  }])

观看表演时的结果与上图相同.您应该使用简单的网络服务器来发出GET请求.

The results when you watch the performance are the same as in the picture above. You should use a simple webserver in order to make the GET request.

推荐答案

根据此问题,更具体地说是此评论,是由在录制性能时间表时,Chrome不会垃圾回收监听器.

According to this issue, more specifically this comment, it's caused by Chrome not garbage collecting listeners during recording of the performance timeline.

这篇关于JavaScript侦听器不断增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆