Why PhantomJS render page use window.setTimeout

我的未来我决定 提交于 2020-01-14 12:58:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!