ajax - Javascript Deconstructor - Stack Overflow

Is there any way that I can define a destructor for an object in JavaScript that will be called automat

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
Add a ment  | 

3 Answers 3

Reset to default 3

You 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

相关推荐

  • ajax - Javascript Deconstructor - Stack Overflow

    Is there any way that I can define a destructor for an object in JavaScript that will be called automat

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信