Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is this setTimeout not working [duplicate]

Tags:

javascript

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

like image 325
Gilles Avatar asked Dec 04 '22 09:12

Gilles


1 Answers

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>
like image 132
Richard Dalton Avatar answered Jan 02 '23 17:01

Richard Dalton