PhantomJS - evaluateAsync()



This method evaluates the given function asynchronously within the page without blocking current execution. This function helps to execute certain scripts asynchronously.

The evaluateAsync method takes arguments as function and second arguments takes time in milliseconds. It is the time taken before the function should execute. This function does not have any return value.

Syntax

Its syntax is as follows −

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

Example

Let us look at an example of the evaluateAsync () method.

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> 

The above program generates the following output.

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