0

I have the following script from a post from Dennis from this URL

It's exactly what i'm looking for but i need to be able to open each url 20 or 30 seconds apart.

<script>
function openWindow(){
    var x = document.getElementById('a').value.split('\n');
    for (var i = 0; i < x.length; i++)
        if (x[i].indexOf('.') > 0)
            if (x[i].indexOf('://') < 0)
                window.open('http://'+x[i]);
            else
                window.open(x[i]);
}
</script>

Can anybody help?

1 Answers1

0

To avoid freezing the whole browser (with a delay function) you can use setTimeOut to execute a function 20000 milliseconds apart.

All the setTimeout are executed right after each other. There is a function scheduled at each atTime milliseconds. Each loop the atTime parameter is increased by 20000 (20 seconds). After that your page stays dormant and the setTimeout functions are executed at the assigned times.

Note the setTimeout("window.open('" + site + "')", atTime);. We can't do setTimeout(window.open(site), atTime); because the function between setTimeout is evaluated at execution time and the variable site will have the last value of the loop. So we do a setTimeout("window.open('xxx')", atTime); where xxx changes in the loop. This way the variable is set in the execution command. (Hope i'm clear enough)

Here is the script:

<script>
function openWindow(){
    var x = document.getElementById('a').value.split('\n');
    atTime = 0;
    for (var i = 0; i < x.length; i++) {
      if (x[i].indexOf('.') > 0) {
        site = x[i];
        if (x[i].indexOf('://') < 0) { site = 'http://' + x[i]; }
        setTimeout("window.open('" + site + "')", atTime);
        atTime += 20000;
      }
    }
}
</script>
Rik
  • 13,565