當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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>

上述程序生成以下內容output

welcome to cookie example
page is loaded 
Hi! I'm evaluateAsync call!

相關用法


注:本文由純淨天空篩選整理自 PhantomJS - evaluateAsync()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。