1

I am sure this is a generic question been asked many times, can't find solution though.

I have javascript using setTimeout() function to close a popup window I created after a set time.

Issue: if I call setTimeout() function within the same script as the one that created the popup window, the popup window does not display the contents of the window, instead the whole thing functions like a single script and the window closes. Its like you need to interrupt the script somehow to get it to parse each section of script before doing the setTimeout.

Does anyone know why or have a workaround?

<script>
var w;
function closeWindow(){ setTimeout(w.close();, 10000);}

function createWindow(){
//create the popup window.

w=window.open("","",'width=200,height=100');

// put something into the popup window
try{w.document.write('<html><head></head><body><p>the w window</p></body>  
<html>')}catch(err){
//handle error here
}

closeWindow();

//closes the createWindow() function
}
</script>
John
  • 346
  • 1
  • 9
  • 19

3 Answers3

1

Proper syntax for using setTimeout:

setTimeout(function() {
    w.close();
}, 10000);

The function gets either string literal which it then evaluates or the better option for executing commands is a function: it can be name of existing function (see example below) or like the above, anonymous function.

Example for using the function in different way:

window.setTimeout(closeWindow, 10000);

Where the function will be:

function closeWindow() {
    w.close();
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
1

Try the following:

setTimeout(w.close, 10000);
1

I played around with your script. The following is tested and functioning:

var w;
function closeWindow(){ setTimeout("w.close();", 10000);}

function createWindow(){
//create the popup window.

w=window.open("","",'width=200,height=100');

// put something into the popup window
try{w.document.write('<html><head></head><body><p>the w window</p></body> <html>')}catch(err){
//handle error here
}
closeWindow();
//closes the createWindow() function
}

If I remember correctly, the quotes are required around the code parameter of your setTimeout function because otherwise you would be passing the return value of the code as an argument for setTimeout rather than the code it needs to run.

Hope this helps.

sdo
  • 652
  • 3
  • 13
  • Correctomundo, my kindom for two f***ing apostrophes. Thanks. – John Nov 07 '11 at 11:11
  • For the record - passing string literal to `setTimeout` is equivalent to using `eval()` thus not really good practice. – Shadow The GPT Wizard Nov 07 '11 at 11:23
  • @ShadowWizard Thanks for your input - I'm not familiar with what you're referring to.. could you flesh out your comment a bit so that anyone who comes across this answer will know what they're getting into by using it? -->John No problem, glad I could help.. strangely I posted a question related to setTimeout about 1 minute before your own. – sdo Nov 07 '11 at 12:02
  • Sure thing @sdo - just read [this excellent answer](http://stackoverflow.com/questions/2949234/formatting-this-javascript-line/2949260#2949260) by Andy. And [another related question](http://stackoverflow.com/questions/6081560/is-there-ever-a-good-reason-to-pass-a-string-to-settimeout) :-) – Shadow The GPT Wizard Nov 07 '11 at 12:10