4

I'm just getting into Java. I'm working on a simple script to open a window, then close it after a short delay. I've tried various connotations of the following with no avail. The function works (in that it opens, then closes the window) but the delay fails to happen.

function manualWindow(){
testWindow = window.open("popup.php","interaction","resizable=0,width=800,height=600,status=0");
setTimeout(testWindow.close(),5000);
}

thank you

Gilles
  • 41
  • 1
  • 1
  • 2
  • 1
    possible duplicate of [Calling functions with setTimeout()](http://stackoverflow.com/questions/3800512/calling-functions-with-settimeout), [Javascript setTimeout() is ignoring the timeout length](http://stackoverflow.com/questions/4120781/javascript-settimeout-is-ignoring-the-timeout-length), – outis Nov 09 '11 at 21:34
  • it would be nice if you accepted the correct answer that was provided to you – tkit Aug 16 '12 at 09:37

4 Answers4

19

You want:

setTimeout(function() { testWindow.close(); },5000);

You current code is executing that function as soon as it is hit and then trying to run it's return value after the delay. By wrapping it up in the function it will be run correctly after 5 seconds.

Example:

<html>
<head></head>
<body>
<script type="text/javascript">
    function manualWindow(){
       testWindow = window.open("http://www.google.co.uk","interaction","resizable=0,width=800,height=600,status=0");
       setTimeout(function() { testWindow.close() },5000);
    }

    manualWindow();
</script>
</body>
</html>
Richard Dalton
  • 34,315
  • 6
  • 69
  • 88
0

Firstly, you're defining your code within an anonymous function. This construct:

(function() {
  ...
)();

does two things. It defines an anonymous function and calls it. There are scope reasons to do this but I'm not sure it's what you actually want.

You're passing in a code block to setTimeout(). The problem is that update() is not within scope when executed like that. It however if you pass in a function pointer instead so this works:

(function() {
  $(document).ready(function() {
    update();
  });
});

because the function pointer update is within scope of that block.

But like I said, there is no need for the anonymous function so you can rewrite it like this:

David
  • 2,844
  • 2
  • 26
  • 47
vargas
  • 1
0

You are not using setTimeout correctly. Try this:

function manualWindow(){
   testWindow = window.open("popup.php","interaction","resizable=0,width=800,height=600,status=0");
   setTimeout(function() { testWindow.close(); },5000);
}
Richard H
  • 34,219
  • 33
  • 105
  • 133
-2

I think your code is missing the qutoes in the first parameter testWindow.close().This can be corrected in the following way:

function manualWindow() {
    testWindow = window.open("http://www.google.com", "interaction", "resizable=0,width=800,height=600,status=0");
    setTimeout('testWindow.close()',5000);
}

manualWindow();

I found this link may help you a bit https://developer.mozilla.org/en/DOM/window.setTimeout

Bertrand Marron
  • 19,324
  • 8
  • 52
  • 88