简体   繁体   中英

loop between links . is opening simultaneous multiple pages and doesn't stop

I know that is a stupid question,but i can't figure it out.(I'm learning, so please be kind )
It's opening simultaneous multiple pages and doesn't stop when var page2 is empty, still continue opening blank page.
I think the problem whit blank page is if(page.length===0) loop(); , but if i use if(page.length===0) return; how can i call the loop function when is done?

I want that after is finish to push the links in var page2, to start loop between that links one by one

    var page = [ "link1" , "link2" , "link3"];
    var page2 = [];
    function pushToPage2Arr() {
        if(page.length===0) loop();
        var url = page.shift();
        var openWindow = window.open(url, "_blank","height=500","width=500");
        setTimeout(function () {
            var cat = openWindow.document.getElementsByClassName("list-id");
            for (var i = 0; i < cat.length ; i++) {
                var id = openWindow.document.getElementsByClassName("list-id")[i].children[0].children[1].textContent;
                var cbn = openWindow.document.getElementsByClassName("list-cbn")[i].children[3].textContent;
                page2.push("http://blabla.com?id="+id+"&cbn="+cbn);
            }
            openWindow.close();
            pushToPage2Arr();
        }, 2000);
    }

    pushToPage2Arr();

    function loop() {
        if(page2.length===0) return;
        var url2 = page2.shift();
        var openWindow2 = window.open(url2, "_blank","height=500","width=500");
        setTimeout(function () {
            //do someting
            openWindow2.close();
            loop();
        }, 3000);
    }

So you want to exhaust page1, populating page2 in the process, and then you want to exhaust page2.

You could just correct the error you're already focused on -- that control returns to pushToPage2Arr from loop:

if(page.length===0) loop();
var url = page.shift();
...

Should be:

if(page.length===0) { loop(); return; }
var url = page.shift();

But what you have here is really a 'delayed for-each'. So why not just write that?

Here's a single page script that sets a span to each number in a list, and then sets it to 'done' after the loop ends.

<!doctype html>

<p>hello: <span id=text></span></p>

<script>
;(function() {
  function delayedForEach(list, fun, timeout, andthen) {
    function process() {
      var x = list.shift()
      if (x === undefined) {
        andthen()
      } else {
        fun(x)
        setTimeout(process, timeout)
      }
    }
    process()
  }

  var stuff = [1,2,3,4],
      text = document.getElementById('text')
  delayedForEach(stuff, function(x) { text.textContent = String(x) }, 500,
    function() { text.textContent = 'done' })
})()
</script>

You could also zip both lists with their corresponding functions, and end with with a dummy object and corresponding finalizer:

function delayedDo(list, delay) {
  function process() {
    var x = list.shift()
    if (x !== undefined) {
      x[1](x[0])
      setTimeout(process, delay)
    }
  }
  process()
}

var text = document.getElementById('text'),
    num = function(x) { text.textContent = '(number: ' + x + ')' },
    str = function(x) { text.textContent = '(string: ' + x + ')' },
    done = function() { text.textContent = 'done!' }
    joblist =
      [ [ 1, num ],
        [ 2, num ],
        [ 3, num ],
        [ 'hello', str ],
        [ 'world', str ],
        [ null, done ] ]

delayedDo(joblist, 1000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM