Is there any way that I can define a destructor for an object in JavaScript that will be called automatically when object is discarded?
Im creating my obj like so:
function SomeObject(){
this.test = null;
this.method = function(){
alert("Testing Method");
};
this.destroy = function(){
// Destroy stuff here
};
}
var test = new SomeObject();
I can call destroy when needed but when the user exits the page I cant call destroy. The reason I need to do this is that I call functions in php using ajax that saves session data. I would like it to destroy the particular session data when im done with that particular js object.
Any ideas?
You can’t use a deconstructor for an object, but you can use
window.onbeforeunloadorwindow.onunloadto do any last-moment adjustments.If you return a string from
onbeforeunloadit will prompt the user that string as aconfirmdialog as to whether they want to leave. This is useful primarily if you want to prompt the user to save their work or some similar state-preserving action.