Reputation: 13
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
Upvotes: 0
Views: 1038
Reputation: 56
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);
Upvotes: 2