0

I have popup window which should alert you that in 10 seconds this popup window will close, it shows the popup window but setTimeout doesn't work, it closes by clicking the button, but itself in 10 seconds it won't close. What i'm doing wrong? I tried set setTimeout function separately , or by string, none of the works.

P.s: i know i can do it with alert, but it won't seem pretty as popup window:)

function myPop() { 
    this.square = null;
    this.overdiv = null;
    this.popIn = function() {
        if (this.square != null) {
            document.body.removeChild(this.square); 
            //setTimeout('alert("прошла секунда")', 10000);
            this.square = null;
        }
        if (this.overdiv != null) {
            document.body.removeChild(this.overdiv);
            //setTimeout('alert("прошла секунда")', 10000);
            this.overdiv = null;
        }
    }
    this.popOut = function(msgtxt) {
        //filter:alpha(opacity=25);-moz-opacity:.25;opacity:.25;
        this.overdiv = document.createElement("div");
        this.overdiv.className = "overdiv";

        this.square = document.createElement("div");
        this.square.className = "square";
        this.square.Code = this;
        var msg = document.createElement("div");
        msg.className = "msg";
        msg.innerHTML = msgtxt;

        this.square.appendChild(msg);
        // var closebtn = document.createElement("button");
      /**closebtn.onclick = setTimeout(function(){
          this.parentNode.Code.popIn();},10000);*/

        setTimeout(function() {
            this.parentNode.Code.popIn();},10000);


        //closebtn.innerHTML = "Жабу";
        //this.square.appendChild(closebtn);

        document.body.appendChild(this.overdiv);
        document.body.appendChild(this.square);
    }
}

any help would be very appreciated

Deepak Goswami
  • 2,030
  • 1
  • 16
  • 22
lp_4eva
  • 13
  • 5

1 Answers1

2

Your callback function within setTimeout is accessing the incorrect this. Within setTimeout, this will refer to Window. Window does not have a parentNode. Try this:

var that = this;
setTimeout(function() {
  that.parentNode.Code.popIn();
}, 10000);
  • Also, when a pop up window opens it freezes the events on the event table from firing. You cannot use setTimeout to close a window because you must click to close the window first, before items on the event table will continue to execute. – jamesyothers Jul 02 '14 at 06:08
  • ok,but there is no other way to create popup window with auto close? – lp_4eva Jul 02 '14 at 06:20
  • Here is the long answer: http://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome – jamesyothers Jul 02 '14 at 07:05