1

I have a problem with this script. It is supposed to load some links (line by line) saved in prova.txt then pass the links one by one to CasperJS and get the html of the page. I know that there must be some problem with timeouts/JavaScript.

Here is the script:

var fs = require('fs');
var file_h = fs.open('prova.txt', 'r');
var line = file_h.readLine();
var links = new Array();
var casper = require('casper').create();

while(line) {
    line = file_h.readLine();
    links.push(line);
}

(function theLoop (i) {
    console.log("LOOP");
    casper.start(links[i], function() {
        setTimeout(function () {
            fs.write("stats" + i + ".html", this.getHTML() );
            i = i + 1;
            if (--i) {
                theLoop(i);
            }
        }, 2000);
    });
    casper.run();
})(4);

Documentation that I used: http://scottiestech.info/2014/07/01/javascript-fun-looping-with-a-delay/

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
ohdecas
  • 29
  • 4
  • Documentation that I used: http://scottiestech.info/2014/07/01/javascript-fun-looping-with-a-delay/ – ohdecas Nov 24 '15 at 09:52

1 Answers1

1

Don't call start and run multiple times on the same casper instance.

casper.start();

(function theLoop (i) {
    console.log("LOOP");
    casper.thenOpen(links[i], function() {
        this.wait(2000, function () {
            fs.write("stats" + i + ".html", this.getHTML() );
            if (--i) {
                theLoop(i);
            }
        });
    });
})(4);

casper.run();

Additionally, it seems like you want to decrease i to 0, so you shouldn't increment (i = i + 1) and decrement it --i in the next line.

Keep in mind that if you use setTimeout in a CasperJS script, you're breaking out of the stepped control flow and have to catch the broken off execution somehow. Use CasperJS' capabilities until it becomes inevitable. For example, I replaced setTimeout(function(){}, x) with casper.wait(x, function(){}).

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • About i, yeah that's a big mistake by myself... thank you. But what do you mean with start and run? Wich one I have to remove and how to do it? – ohdecas Nov 24 '15 at 09:58
  • The code I've provided should work. I've also found another issue in the mean time. – Artjom B. Nov 24 '15 at 10:06
  • Thank you it works ;) Now I just need to understand where is the big change looking at my previous code.. – ohdecas Nov 24 '15 at 10:11
  • I moved `start` and `run` out of the recursive function into global scope, fixed the `i++`/`--i` issue and stayed in the control flow. If you want to understand more about how CasperJS' steps work, then look at my answer here: [What must be wrapped in then() statements in CasperJS? How to determine execution order of sync/async functions?](http://stackoverflow.com/a/30129100/1816580) – Artjom B. Nov 24 '15 at 10:18