Skip to main content

Bug Tracker

Side navigation

#1136 closed bug (fixed)

Opened April 20, 2007 01:00PM UTC

Closed May 31, 2007 03:54AM UTC

Events not removed from jQuery.event.global

Reported by: brandon Owned by:
Priority: minor Milestone: 1.1.3
Component: event Version: 1.1.2
Keywords: Cc:
Blocked by: Blocking:
Description

From Michael Schwarz on the discuss list:

I have a very simple page which will be refreshed from time to time. I

build an example which will be called every 1000 msec to redner a html

table with a button inside. The button click event is set with the

bind method. This causes a memory problem.


<div id="display">
</div>

<script type="text/javascript">

function clickhandler() {
       alert("clicked");
}

function render() {
       var d = new Date();
       var n = d.getTime();

       $("button").unbind("click", clickhandler);

       var sb = [];
       sb.push("<table>");
       for(var i=0; i<100; i++) {
               sb.push("<tr><td>" + i + "</td><td>" + n + "</td><td><button>Click</
button></td></tr>");
       }
       sb.push("</table>");

       $("#display").html(sb.join(''));
       $("button").bind("click", clickhandler);

       setTimeout(render, 1000);
}

$(window).ready(render);

</script>

I see the problem, now. The array jQuery.event.global is getting

bigger and bigger. For a long running Web application (without any

page refresh) it is a strange problem. If I have removed an event and

then will delete the element (the DOM element) this should be removed

from the jQuery.event.global array. If I press F5 it will clear nearly

everything and memory usage is like before.

I added some lines to the remove function (line 1245):

// original
if(!k) element["on" + type] = null;

// changed to
if (!k) {
   element["on" + type] = null;

   for ( var i=0; i<this.global[type].length; i++) {
       if(this.global[type][i] == element) {
           this.global[type].splice(i, 1);
           break;
       }
   }
}

I see that the for loop isn't very well, maybe there is another way to

remove the element if there is no handler.

Attachments (0)
Change History (2)

Changed May 25, 2007 05:37AM UTC by carlos.aguay comment:1

You shouldn't break when you find the first element. You could attach several events of the same type to the same element. So 'element' could be present more than once in the array. Therefore it should be something like:

var o = this.global[type];
for (var i = o.length; i >= 0; i--) {
  if (o[i] == element) {
    o.splice(i,1);
  }
}

Changed May 31, 2007 03:54AM UTC by brandon comment:2

description: From Michael Schwarz on the discuss list:\ \ I have a very simple page which will be refreshed from time to time. I\ build an example which will be called every 1000 msec to redner a html\ table with a button inside. The button click event is set with the\ bind method. This causes a memory problem.\ \ {{{\ \ <div id="display">\ </div>\ \ <script type="text/javascript">\ \ function clickhandler() {\ alert("clicked");\ }\ \ function render() {\ var d = new Date();\ var n = d.getTime();\ \ $("button").unbind("click", clickhandler);\ \ var sb = [];\ sb.push("<table>");\ for(var i=0; i<100; i++) {\ sb.push("<tr><td>" + i + "</td><td>" + n + "</td><td><button>Click</\ button></td></tr>");\ }\ sb.push("</table>");\ \ $("#display").html(sb.join(''));\ $("button").bind("click", clickhandler);\ \ setTimeout(render, 1000);\ }\ \ $(window).ready(render);\ \ </script>\ \ }}}\ \ I see the problem, now. The array jQuery.event.global is getting\ bigger and bigger. For a long running Web application (without any\ page refresh) it is a strange problem. If I have removed an event and\ then will delete the element (the DOM element) this should be removed\ from the jQuery.event.global array. If I press F5 it will clear nearly\ everything and memory usage is like before.\ \ I added some lines to the remove function (line 1245):\ \ {{{\ // original\ if(!k) element["on" + type] = null;\ \ // changed to\ if (!k) {\ element["on" + type] = null;\ \ for ( var i=0; i<this.global[type].length; i++) {\ if(this.global[type][i] == element) {\ this.global[type].splice(i, 1);\ break;\ }\ }\ }\ \ }}}\ \ I see that the for loop isn't very well, maybe there is another way to\ remove the element if there is no handler.From Michael Schwarz on the discuss list: \ \ I have a very simple page which will be refreshed from time to time. I \ build an example which will be called every 1000 msec to redner a html \ table with a button inside. The button click event is set with the \ bind method. This causes a memory problem. \ \ {{{ \ \ <div id="display"> \ </div> \ \ <script type="text/javascript"> \ \ function clickhandler() { \ alert("clicked"); \ } \ \ function render() { \ var d = new Date(); \ var n = d.getTime(); \ \ $("button").unbind("click", clickhandler); \ \ var sb = []; \ sb.push("<table>"); \ for(var i=0; i<100; i++) { \ sb.push("<tr><td>" + i + "</td><td>" + n + "</td><td><button>Click</ \ button></td></tr>"); \ } \ sb.push("</table>"); \ \ $("#display").html(sb.join('')); \ $("button").bind("click", clickhandler); \ \ setTimeout(render, 1000); \ } \ \ $(window).ready(render); \ \ </script> \ \ }}} \ \ I see the problem, now. The array jQuery.event.global is getting \ bigger and bigger. For a long running Web application (without any \ page refresh) it is a strange problem. If I have removed an event and \ then will delete the element (the DOM element) this should be removed \ from the jQuery.event.global array. If I press F5 it will clear nearly \ everything and memory usage is like before. \ \ I added some lines to the remove function (line 1245): \ \ {{{ \ // original \ if(!k) element["on" + type] = null; \ \ // changed to \ if (!k) { \ element["on" + type] = null; \ \ for ( var i=0; i<this.global[type].length; i++) { \ if(this.global[type][i] == element) { \ this.global[type].splice(i, 1); \ break; \ } \ } \ } \ \ }}} \ \ I see that the for loop isn't very well, maybe there is another way to \ remove the element if there is no handler.
resolution: → fixed
status: newclosed

This is now fixed in Rev [2011].