title: How to for loop in casperjs
body: <p>I am trying to click a 'next' button N number of times and grab the page source each time.  I understand that I can run an arbitrary function on the remote website, so instead of click() I just use the remote function nextPage()  How do I run the following, an arbitrary number of times:</p>
  
  <pre><code>var casper = require('casper').create();
  
  casper.start('http://www.example.com', function() {
  
      this.echo(this.getHTML());
      this.echo('-------------------------');
  
      var numTimes = 4, count = 2;
  
      casper.repeat(numTimes, function() {
          this.thenEvaluate(function() {
              nextPage(++count);
          });
  
          this.then(function() {
              this.echo(this.getHTML());
              this.echo('-------------------------');
          });
      });
  
  });
  </code></pre>
  
  <p>'i' here is an index I <em>tried</em> to use in a javascript for loop.</p>
  
  <p>So tl;dr: I want lick 'next', print pages source, click 'next', print page source, click 'next'... continue that N number of times.</p>