我们如何在phantomjs中使用evaluateAsync [英] how we can use evaluateAsync in phantomjs

查看:130
本文介绍了我们如何在phantomjs中使用evaluateAsync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

evaluateAsync 的用法是什么?当我们必须使用此函数时,使用此函数有什么好处。
在下面我们看到了一个很差的文档

what's the usage of evaluateAsync and when we have to use this function and what's the benefit of using this function . in the below we see a poor documentation for this :

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.evaluateJavaScript example.

任何机构都可以显示 evaluateAsync in phantomjs

any body can show a example of usage of evaluateAsync in phantomjs

推荐答案

此函数允许您执行任何JavaScript代码,例如 evaluate API函数。
但它会异步评估你的代码。这意味着:

This function allows you to execute any JavaScript code like the evaluate API function. But it will evaluate your code asynchronous. It means:


  • 当前执行上下文不会被阻止。

  • 它不会返回任何结果。

假设你想要执行一些长期运行的JavaScript代码,但你对它的结果不感兴趣。如果您将使用 evaluate ,您的当前执行上下文将被阻止。

Let's say you want execute some long-running JavaScript code, but you don't interested in its result. If you will use evaluate, your current execution context will be blocked.

evaluateAsync 有点不对劲。 evaluateAsync 的正确签名是:
evaluateAsync(function,ms,args),其中:

The documentation for evaluateAsync is a bit wrong. The correct signature for evaluateAsync is: evaluateAsync(function, ms, args), where:


  • function - 要评估的函数

  • ms - 执行前等待的时间

  • args - 函数参数

示例:

evaluateAsync(function() {
   console.log('Hi! I\'m evaluateAsync call!');
}, 1000);

在现实世界中使用:


  • 您想要捕获一些异步事件。

  • 单元测试! AFAIK,PhantomJS跑步者使用 evaluateAsync 来进行单位测试。

  • You want to capture some asynchronous events.
  • Unit testing! AFAIK, PhantomJS runners use evaluateAsync to run unit tests.

这篇关于我们如何在phantomjs中使用evaluateAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆