简体   繁体   English

PhantomJS javascript等到功能完成

[英]PhantomJS javascript wait until function complete

I have a problem with a PhantomJS script. 我的PhantomJS脚本有问题。 The script gets a JSON encoded string from a web page and does other things with it. 该脚本从网页获取JSON编码的字符串,并执行其他操作。 The script: 剧本:

var address = address;
var amount = 0;

function changeAmount()
{
   var page=require('webpage').create();
   page.open (address, function(){
     //parse json, set amount to something (usually 4)
     amount = 4;
  });
}

changeAmount();
console.log(amount); //prints 0

//Do stuff with amount

phantom.exit(); //amount not changed yet.

How can I check if the changeAmount function is finished before going forward? 如何在前进之前检查changeAmount函数是否完成? Timeout is not possible since I don't know the time it takes to process the changeAmount. 无法超时,因为我不知道处理changeAmount所花费的时间。

You can use a callback, like so: 您可以使用回调,如下所示:

function changeAmount(callback) {
    var page=require('webpage').create();
    page.open (address, function () {
        //parse json, set amount to something (usually 4)
        amount = 4;
        callback();
    });
}

changeAmount(function () {
    // This function runs when callback() (above) is reached
    console.log(amount);

    //Do stuff with amount

    phantom.exit();
});

And if you're not using the amount variable elsewhere, you could eliminate it by passing it as an argument to the callback: 如果您不在其他地方使用amount变量,则可以通过将其作为参数传递给回调来消除它:

changeAmount(function (amount) {

and then 接着

callback(amount); // or callback(4);

page.open() is an inherently asynchronous function. page.open()是一个固有的异步函数。 The only reliable way to do this is to use callbacks in the PhantomJS script: 唯一可靠的方法是在PhantomJS脚本中使用回调:

var address = address;

function changeAmount(callback)
{
   var page = require('webpage').create();
   page.open (address, function(){
     //parse json, set amount to something (usually 4)
     var amount = 4;
     callback(amount);
  });
}

You can even go as far as passing amount into that callback to remove the global variable. 您甚至可以将amount传递到该回调中以删除全局变量。

After that, you will need to write your script using that callback pattern. 之后,您将需要使用该回调模式编写脚本。

changeAmount(function(amount){
    console.log(amount);

    //Do stuff with amount

    phantom.exit();
});

Furthermore, you probably shouldn't create a new page every time you call changeAmount() (if you do this repeatedly). 此外,您可能不应该在每次调用changeAmount()都创建一个新page (如果重复执行此操作)。 You can reuse the same page . 您可以重复使用同一page If you think that creating a new page gives you a fresh environment to work in, then you're mistaken. 如果您认为创建新页面为您提供了一个全新的工作环境,那么您就错了。 It is just like a new tab. 就像一个新标签一样。 It will use the same session as all the other pages that you have created. 它将使用与您创建的所有其他页面相同的会话。

If you do this often, this will lead to a memory leak, because you're not closing the previously opened pages. 如果您经常执行此操作,则会导致内存泄漏,因为您没有关闭以前打开的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM