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?
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?
Share Improve this question asked Aug 26, 2011 at 18:29 Adam MagalukAdam Magaluk 1,72620 silver badges30 bronze badges 4- Check out stackoverflow./questions/1631959/browser-window-close-event – Demian Brecht Commented Aug 26, 2011 at 18:32
- if you have a destroy() method, why don't you just call it when you're done with the object? – Einacio Commented Aug 26, 2011 at 18:34
- Einacio, I will but but if the user exits the page or changes pages within the application I cant call it. I will try the window.unload though. – Adam Magaluk Commented Aug 26, 2011 at 18:38
- No. There is no such thing as a "destructor" in JavaScript and using onunload/onbeforeunload is not reliable across browsers/situations (consider history navigation or refresh as simple counter-examples). Consider alternative approaches. – user166390 Commented Aug 26, 2011 at 18:42
3 Answers
Reset to default 3You can't use a deconstructor for an object, but you can use window.onbeforeunload
or window.onunload
to do any last-moment adjustments.
If you return a string from onbeforeunload
it will prompt the user that string as a confirm
dialog 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.
You can call that function on the window.onunload
or window.onbeforeunload
events. But be aware that this might be called even when the user navigates within your own application or when the back and forward buttons are used.
You can bind to the unload event:
function SomeObject(){
...
var that = this;
$(window).unload(function() {
that.destroy();
});
}
This way your destroy method will be called when the user quits the page.
Without jQuery you can bind the event with this:
var event = 'load';
var handler = function() { ... };
if (window.addEventListener) {
window.addEventListener(event, handler, false);
} else {
window.attachEvent('on'+event, handler);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744222247a4563838.html
评论列表(0条)