PhantomJS - evaluateAsync()



此方法在不阻止当前执行的情况下,异步地在页面内评估给定的函数。此函数有助于异步执行某些脚本。

evaluateAsync 方法将参数作为函数,第二个参数以毫秒为单位表示时间。这是在函数执行之前所需的时间。此函数没有任何返回值。

语法

其语法如下 -

evaluateAsync(function, [delayMillis, arg1, arg2, ...]) 

例子

我们来看一个 evaluateAsync() 方法的示例。

var wpage = require('webpage').create(); 
wpage.onConsoleMessage = function(str) { 
   console.log(str); 
} 
wpage.open("http://localhost/tasks/content.html", function(status) { 
   wpage.evaluateAsync(function() { 
      console.log('Hi! I\'m evaluateAsync call!'); 
   }, 1000); 
}); 

content.html

<html> 
   <head>
      <title>welcome to phantomjs</title>
   </head> 

   <body name = "content"> 
      <script type = "text/javascript"> 
         window.name = "page2"; 
         console.log('welcome to cookie example'); 
         document.cookie = "username = Roy; expires = Thu, 22 Dec 2017 12:00:00 UTC"; 
         
         window.onload =  function() { 
            console.log("page is loaded"); 
         } 
      </script> 
      
      <iframe src = "http://localhost/tasks/a.html" width = "800" height = "800" 
         name = "myframe" id = "myframe"></iframe> 
      <h1>dddddddddd</h1> 
   </body>
   
</html> 

以上程序生成以下输出

welcome to cookie example
page is loaded 
Hi! I'm evaluateAsync call!
phantomjs_webpage_module_methods.htm
广告