Destroy a function in javascript (jquery)

Is there anyone who knows how to destroy the javascript (jquery) function? I use jquery "selectable" and the call to the "edit" function is fired on the selected "stop" event.

Inside this edit function, I have nested switch functions with a lot of click events, and I have many functions in every click event. My problem is every time I run the "selectable" functions, and the events inside the "edit" function are fired again, but the previous functions and events still exist. Now I have to cancel each event in the “edit” function on the selected “start”.

Is this a memory leak problem? and is there a way to “destroy” functions in javascript? I tried to declare the function null when the function ends, but this will not work. functions and events within it still exist.

Who has a clue?

demo page here → http://dreamerscorp.com/test/test01/javascript_destory_test.html

edit 2009/10/31 :) Thank you very much for your advice, your comments are very useful to me, thanks again!

+5
source share
4 answers

Basically, you need to remove all references to these functions so that the JavaScript garbage collector can collect them. If they are connected, you need to untie them. If they have other variables, they should be set to zero.

, - ; .

... EDIT:

, , :

    function edit(){
      $('.edit').click(function(){
        //...
        function _edit(boxTitle,selectedItemAmount){
          //...
          $('#box .yes').click(function(){
            alert(boxTitle + ' for ' + selectedItemAmount + ' selected item');
            $('#msg').hide(); // hide msg box when yes btn is clicked
          });
        }
        //...
        $('#box .no').click(function(){
          $('#msg').hide();
        });
      });

, : " DOM", . JavaScript , .

, - , :

    function boxClickYes(e) {
      alert(e.data.boxTitle + ' for ' + e.data.selectedItemAmount +
        ' selected item');
      $('#msg').hide(); // hide msg box when yes btn is clicked
    }
    function boxClickNo(e) {
      $('#msg').hide();
    }
    function edit(){
      $('.edit').click(function(){
        //...
        function _edit(boxTitle,selectedItemAmount){
          //...
          $('#box .yes').bind("click", {boxTitle: boxTitle,
            selectedItemAmount: selectedItemAmount}, boxClickYes);
        }
        //...
        $('#box .no').click(boxClickNo);
      });

, data jQuery , , , ( , ). , (, $.each), , .

+3

, :

myFunction = null;
// or 
myFunction = function () {};

:

var autoDestroy = function () {
  autoDestroy = null;
  //...
  return 1;
};

autoDestroy(); // returns 1
autoDestroy(); // TypeError: autoDestroy is not a function
+7

jquery, unbind http://docs.jquery.com/Events/unbind

$("something").click(function() {
    $("anotherOne").click(function() {
         ....
    });
});

, , "-", "anotherOne", , , .

$("something").click(function() {
    $("anotherOne").unbind('click').click(function() {
         ....
    });
});

"anotherOne".

.

+6

"" javascript, , . . , , , javascript ( ), , : , .

See javascript delete operator as one way to delete a variable or member of an object. Setting the value to null / undefined / other-object is deleted by the method of reaching the previously specified object (although otherwise it may be available and, therefore, will not be restored), but will not get rid of the variable / member.

delete variable
delete obj.member
delete obj[member]
+1
source

All Articles