markzzz
markzzz

Reputation: 47985

How can I remove a function and all object/handlers in it?

I have a function like this:

var InitScrollbar = function () {
... variables, object, handlers, ecc
}

and on resize I'd like to destroy it and all variables/handlers/objects inside it. Tried with:

$(window).on("resize", function (e) {
    InitScrollbar = null;
    //InitScrollbar();
});

but the handler inside it, after a resize, works again. How can I destroy all?

Upvotes: 1

Views: 101

Answers (2)

LittleSweetSeas
LittleSweetSeas

Reputation: 7074

You could implement a sort of dispose method to invoke, eg:

var InitScrollbar = function () {
   // variables, object, handlers, ecc
   this.Dispose = function(){
      //clear / detach all your handlers here
   }
}

and then use it this way:

$(window).on("resize", function (e) {
    InitScrollbar.Dispose();
    //InitScrollbar();
});

EDITED: As you aren't instancing InitScrollbar, you should change approach, try this:

var InitScrollbar = function () {
   // variables, object, handlers, ecc   
}

var DisposeScrollbar = function(){
   //clear / detach all your handlers here
   //take care of correctly reference to InitScrollbar properties
}

and then use it this way:

$(window).on("resize", function (e) {
    DisposeScrollbar();
});

Upvotes: 1

qwertynl
qwertynl

Reputation: 3933

You would have to manually remove all of the handlers.


What you could do is clone the elements without the handlers and replace the old elements with the new cloned ones, but I would not advise this method.

Upvotes: 0

Related Questions