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>
I played around with your script. The following is tested and functioning:
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.