PDA

View Full Version : Time delay between window.open



cbiblis
01-29-2011, 01:17 AM
Can someone help me please? here is the script i'm working on:


<script type="text/javascript">
function open_win() {
window.open("Blank");
}
</script>
</head>
</script>
<form name = "myform">
<select name = "numberWindows" onchange = "openThem()">
<option value = "0">Choose....</option>
<option value = "5">5</option>
<option value = "10">10</option>
<option value = "15">15</option>
<option value = "20">20</option>
<option value = "25">25</option>
<option value = "30">30</option>
<option value = "35">35</option>
<option value = "40">40</option>
</select>
</form>
<script type = "text/javascript">
var i;
function openThem() {
var val = parseInt(document.myform.numberWindows.value);
for (i =0; i < val; i++) { open_win();}
}
</script>
</body>
</html>

The script currently asks for the amount of tabs to open then opens them when the amount is selected.
What i need to do is slow down the time between each open window to like 1000 millisecs i have tried using thread.sleep but then it only opens 1 tab.

Old Pedant
01-29-2011, 01:51 AM
You'll love this...


<html>
<head>
<script type="text/javascript">
function openThem( count )
{
for ( var i = 1; i <= count; ++i )
{
setTimeout( functiion() { window.open("Blank"); }, i * 1000 );
}
}
</script>
</head>
<body>
<form name="irrelevant">
<select name="ignored" onchange="openThem(parseInt(this.value))">
<option value = "0">Choose....</option>
<option value = "5">5</option>
<option value = "10">10</option>
<option value = "15">15</option>
<option value = "20">20</option>
<option value = "25">25</option>
<option value = "30">30</option>
<option value = "35">35</option>
<option value = "40">40</option>
</select>
</form>
</body>
</html>

p.s.: thread.sleep is for *JAVA*. JavaScript doesn't have one. You were getting an error from your script after opening the first window, which is why only one opened!

cbiblis
01-29-2011, 02:01 AM
Thanks for you help, however, I can't get it to run. the popup blocker is stopping it.

Old Pedant
01-29-2011, 02:54 AM
Yes? So turn off the popup blocker.

That's exactly what it's there for...to stop pages like this from popping up lots of windows. If you really want them, you'll have to turn it off.

cbiblis
01-29-2011, 05:16 AM
Well the original script can open as many as it wants without the popup blocker effecting it. what is it about the alteration that throws up the flag?

Old Pedant
01-29-2011, 08:48 PM
Ahhh...you didn't say that. Got me.

We can restore to the original, for all intents and purposes, save for the the timeout:


<script type="text/javascript">
function open_win() {
window.open("Blank");
}
function openThem( )
{
var count = parseInt( document.myform.numberWindows.value );
for ( var i = 1; i <= count; ++i )
{
setTimeout( open_win, i * 1000 );
}
}
</script>
</head>
<body>
<form name = "myform">
<select name = "numberWindows" onchange = "openThem()">
<option value = "0">Choose....</option>
<option value = "5">5</option>
<option value = "10">10</option>
<option value = "15">15</option>
<option value = "20">20</option>
<option value = "25">25</option>
<option value = "30">30</option>
<option value = "35">35</option>
<option value = "40">40</option>
</select>
</form>

I dunno why that would make a difference, but try it.

Old Pedant
01-29-2011, 08:49 PM
If that still doesn't work, I'd have to guess that the browser sees the delayed open as *NOT* a direct result of a user action. Without the delay, it sees the open as a direct action and allows it. If so, then I don't see an answer.

cbiblis
02-01-2011, 04:03 PM
Oh sorry, i didn't expect anymore replys. Thanks for your help. it's working good now.:thumbsup:

Krupski
02-01-2011, 08:16 PM
If that still doesn't work, I'd have to guess that the browser sees the delayed open as *NOT* a direct result of a user action. Without the delay, it sees the open as a direct action and allows it. If so, then I don't see an answer.

FYI, I just tested it here (Firefox 3.6.13, WinXP 32). Any delay over 1000 msec. triggers the popup blocker.

If I set the base delay to 500, 2 windows opened (500 and 1000). At 250, 4 windows opened (250, 500, 750, 1000) before triggering the blocker.

-- Roger

Old Pedant
02-01-2011, 10:33 PM
Fascinating! That's probably a pretty good rule of thumb for a popup blocker to use, actually. Nice testing, Krupski!

cbiblis
02-04-2011, 12:16 AM
The only problem that i'm having with the alteration is that it now opens windows instead of tabs in chrome but works in FF. Is there a way to fix this issue? Also when using firefox or chrome when i try to turn off the popup blocker off to allow more than 4 windows it shows "turn popup blocker off for " therefore it doesn't turn the popup blocker off.