javascript - Wait for the end of a function execution in a PhantomJS script -


i working on little program openning big number of webpage (according id taken id.txt) , save in file.

var page = require('webpage').create(); var fs = require('fs'); var file_h = fs.open('id.txt', 'r'); // contains data : myname-1111 var line = file_h.readline();  while(line) {     data = line.split("-");     line = file_h.readline();     savepage(data[1]); }  function savepage(id){     page.open('http://www.mywebsite.com/'+id, function(){         page.evaluate();         fs.write("page/"+id+'.html', page.content, 'w');     }); }  file_h.close(); phantom.exit(); 

at moment, saving html, head , body tag without content.

i think due fact not waiting current page load correctly , completely.

so know if there solution wait between each "for" iteration full page , able save it?

the problem loop execution synchronous, page.open() call in savepage function not. when loop executed, page not loaded, because next page open triggered.

you might think last page loaded, not, because you're exiting phantom.exit().

javascript doesn't have sleep function. waiting/sleeping done asynchronously. way solve use recursion.

move content of while loop inside page.open() call , remove loop. call function. need move finish condition page.open() call:

var page = require('webpage').create(); var fs = require('fs'); var file_h = fs.open('id.txt', 'r'); // contains data : myname-1111  function traverse(){     var line = file_h.readline();     if (!line) {         file_h.close();         phantom.exit();     }     page.open('http://www.mywebsite.com/'+id, function(){         var data = line.split("-");         traverse();         fs.write("page/"+data[1]+'.html', page.content, 'w');     }); }  traverse(); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -