1

I have several URLs in my array and I want to run it one by one, but when I run it in a loop, it executes all at the same time and does not wait.

Here is what I tried:

<html>
    <head>
    <script>
        function work(){
            var otherStoryLinksArray = [];
            otherStoryLinksArray[0] = 'http://google.com';
            otherStoryLinksArray[1] = 'http://www.yahoo.com';
            otherStoryLinksArray[2] = 'http://gmail.com';
            for(var i=0;i<3;i++){   
                var openWindow = window.open(otherStoryLinksArray[i]);  
                setTimeout(function(){openWindow.close();},3000);
            }
        }
    </script>
    </head>
    <body onload=work();>
    </body>
</html>

I want it to open one URL, wait for 30 secs, close the popup, and then start another URL.

Waiting for your reply guys. Any help would be appreciated thanks...

Kate
  • 1,455
  • 3
  • 21
  • 34
Priyank
  • 784
  • 1
  • 13
  • 26
  • possible duplicate of [Python while loop conversion to Javascript](http://stackoverflow.com/questions/14327647/python-while-loop-conversion-to-javascript) – Bergi Mar 27 '14 at 16:33
  • I suggest you open the window, and in the for look you change the location of the newly opened window every x Sec – Martijn Mar 27 '14 at 16:33
  • Get rid of the `for` loop and use `setInterval` instead. – blgt Mar 27 '14 at 16:35
  • Answered here .... http://stackoverflow.com/questions/16873323/javascript-sleep-wait-before-continuing – anu Mar 27 '14 at 16:42

4 Answers4

1

Code: http://jsfiddle.net/WgR4y/1/

Demo: http://jsfiddle.net/WgR4y/1/show/ (make sure you disable popup blocker)

Works for unlimited number of URLs in array.

var otherStoryLinksArray = [
      'http://google.com',
      'http://www.yahoo.com',
      'http://gmail.com'
    ],
    timeToCloseWindow = 3000;

function work() {
    if(otherStoryLinksArray.length==0) return;
    var url = otherStoryLinksArray.shift();
    var openWindow = window.open(url);
    setTimeout(function () {
        openWindow.close();
        work();
    }, timeToCloseWindow);
}

work();
Aamir Afridi
  • 6,095
  • 3
  • 38
  • 41
0

You need to stagger your setTimeout calls and since setTimeout uses ms, 30 s = 30000 ms:

   function work () {
        var otherStoryLinksArray = [];
        otherStoryLinksArray[0] = 'http://google.com';
        otherStoryLinksArray[1] = 'http://www.yahoo.com';
        otherStoryLinksArray[2] = 'http://gmail.com';

        for(var i=0; i<3; i++) {
            var openWindow;
            setTimeout(function () { 
                openWindow = window.open(otherStoryLinksArray[i]);
            }, 30000 * i); //Open this window

            setTimeout(function () {
                openWindow.close();
            }, 30000 * i + 30000); //Close it 30 seconds from "now"
        }
    }
sushain97
  • 2,493
  • 1
  • 22
  • 33
0

Use set interval instead of for-loop

function work(){
        var otherStoryLinksArray = [];
        otherStoryLinksArray[0] = 'http://google.com';
        otherStoryLinksArray[1] = 'http://www.yahoo.com';
        otherStoryLinksArray[2] = 'http://gmail.com';

        var myVar=setInterval(function(){myTimer()},3000);

        var i=0;
        var openWindow;
        function myTimer()
        {
            if(openWindow != null)
                openWindow.close();
            if(i < 3) 
            {                    
                openWindow = window.open(otherStoryLinksArray[i]);  
                i++;
            }
            else
                clearInterval(myVar);
        }

    }
anu
  • 937
  • 15
  • 35
0
var otherStoryLinksArray = [
    'http://google.com',
    'http://www.yahoo.com',
    'http://gmail.com'];
var i = 0;
var openedWindow = null;
function loop(){
    openedWindow && openedWindow.close();
    if(otherStoryLinksArray[i]){
        var openedWindow = window.open(otherStoryLinksArray[i]);  
        i++;
        setTimeout(loop,30000);
    }
}
nelsonec87
  • 106
  • 6