问题
I am currently using PhantomJS for some doc's reports and looking in some examples i found this.
window.setTimeout(function () {
page.render(output);
console.log(output);
phantom.exit();
}, 200);
I wonder why using window.setTimeout?? It affect web rendering??
Thanks... Nick.
回答1:
The page.open callback is invoked only when the page is loaded. This does not means that everything has been loaded in your page and the js has been fully executed.
In addition, the js on the web page may not be executed immediately, especially if the site uses MVC client frameworks like AngularJS or Ember.js : a lot of stuff is done after the page load event.
Using setTimeout give you a little delay to be sure that your page is fully rendered.
Sometimes 200ms is not enought and you have to increase the duration.
Another way is to use the waitfor.js example : waits until a test condition is true such as an element is visisble.
来源:https://stackoverflow.com/questions/22028032/why-phantomjs-render-page-use-window-settimeout