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?

  • Of what user is post 2? – Rik Sep 23 '13 at 11:14
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>
| improve this answer | |
  • it was the script by Dennis. > If i paste in multiple urls in the box of the script i need for example. open www.google.com wait 20 seconds open www.bing.com wait 20 seconds open www.yahoo.com and so on through the list that has been pasted in. Hope it makes sense as my English is not very good and this is my first time with HTML. Thankyou – Louwrens Kokke Sep 23 '13 at 11:38
  • Then you can use the sleep function in my second code in my answer. – Rik Sep 23 '13 at 11:43
  • Thankyou Rik I'll give it a go. This is my first ever HTML edit , wish me luck. :) – Louwrens Kokke Sep 23 '13 at 11:44
  • It's not working as i hoped as a blank white window opens every 20 seconds with " firefox not responding " and it's not until 20 seconds after the last frozen window opens that i then suddenly get all the frozen pages to load all the URLs at once. :( If you have any ideas i will be back on tomorrow to reply. thanks – Louwrens Kokke Sep 23 '13 at 12:17
  • Yeah, already said the delay-function was not recommended. I changed my answer to a function where there is no freezing of the browser. – Rik Sep 23 '13 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.