PhantomJS evaluateAsync () 使用方法


发布日期 : 2022-08-11 16:39:49 UTC

访问量: 10 次浏览

PhantomJS evaluateAsync()

evaluateAsync() 方法在不阻塞当前执行的情况下异步地对页面中的给定函数进行评估。此函数有助于异步执行某些脚本。

evaluteAsync 方法以函数作为参数,并且第二个参数表示以毫秒为单位的延迟时间。这个函数没有返回值。

语法

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!