CasperJS with Recursion (Using Stack)

function getUrlArr(){
  return urlArray;
}

function recursionFunction(){
  while(){
    var url = Stack.pop();
  }

  if(){
    casper.thenOpen(url, function() {
      output(Information);
    }
    if(Stack.lenght > 0){
      // Recursion
      recursionFunction();
    }
  }else{
    output(Information);
    casper.thenOpen(currentUrl, function() {
      var urlArray = this.evaluate(getUrlArr);
      this.each(urlArray, function(self, url) {
        Stack.push(url);
      });
      if(Stack.lenght > 0){
        // Recursion
        recursionFunction();
      }
    });
  }
}

(function() {
  // Declare Stack, IMPORTANT, there is no 'var'
  Stack = [];

  // Init CasperJS
  casper = require('casper').create({});
  casper.start(urlLogin, function() {
  });

  // Init Stack and Start Recursion, and function in recursion CAN USE Stack
  Stack.push(url);
  recursionFunction();

  // Ending
  casper.run(function() {
    this.echo("Finished running ...");
    return this.exit();
  });
}).call(this);

你可能感兴趣的:(CasperJS with Recursion (Using Stack))