Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PhantomJS javascript等待函数完成_Javascript_Asynchronous_Phantomjs_Wait - Fatal编程技术网

PhantomJS javascript等待函数完成

PhantomJS javascript等待函数完成,javascript,asynchronous,phantomjs,wait,Javascript,Asynchronous,Phantomjs,Wait,我对PhantomJS脚本有问题。该脚本从网页获取一个JSON编码的字符串,并使用它执行其他操作。剧本: 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)

我对PhantomJS脚本有问题。该脚本从网页获取一个JSON编码的字符串,并使用它执行其他操作。剧本:

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.

在继续之前,如何检查changeAmount函数是否已完成?超时是不可能的,因为我不知道处理changeAmount所需的时间。

您可以使用回调,如下所示:

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();
});
如果您没有在其他地方使用
amount
变量,可以通过将其作为参数传递给回调来消除它:

changeAmount(function (amount) {
然后

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

您可以使用回调,如下所示:

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();
});
如果您没有在其他地方使用
amount
变量,可以通过将其作为参数传递给回调来消除它:

changeAmount(function (amount) {
然后

callback(amount); // or callback(4);
page.open()
是一个固有的异步函数。唯一可靠的方法是在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);
  });
}
您甚至可以将
amount
传递到该回调以删除全局变量

之后,您将需要使用该回调模式编写脚本

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

    //Do stuff with amount

    phantom.exit();
});

此外,您可能不应该每次调用
changeamunt()
(如果您重复这样做),都创建一个新的
页面。您可以重复使用相同的
页面
。如果你认为创建一个新页面会给你一个新的工作环境,那么你错了。它就像一个新的标签。它将使用与您创建的所有其他页面相同的会话

如果您经常这样做,这将导致内存泄漏,因为您没有关闭以前打开的页面。

page.open()
本质上是一个异步函数。唯一可靠的方法是在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);
  });
}
您甚至可以将
amount
传递到该回调以删除全局变量

之后,您将需要使用该回调模式编写脚本

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

    //Do stuff with amount

    phantom.exit();
});

此外,您可能不应该每次调用
changeamunt()
(如果您重复这样做),都创建一个新的
页面。您可以重复使用相同的
页面
。如果你认为创建一个新页面会给你一个新的工作环境,那么你错了。它就像一个新的标签。它将使用与您创建的所有其他页面相同的会话

如果您经常这样做,这将导致内存泄漏,因为您没有关闭以前打开的页面