从成功处理程序获取结果

问题描述 投票:1回答:3

我正在我的应用程序中列出游戏。有些是付费的,有些是免费的,因此在上市期间,我正在调用一种从Play商店获取价格并在Success回调中获取响应的方法。我的问题是我的成功经理在列出所有游戏之后设定价格,因此所有游戏​​都被列为免费游戏。我的代码是

清单游戏

function render(res)
{
    for (var i = 0; i < res.length; i++) {
        var data=res[i];
        if(data.product_id!='Free' && data.product_id!='')
        {
            getDetails(data.product_id);
        }
        else
        {
            price='Free';
        }
        console.log(price);
        var gameOnScreen = "<li class='game-box showlist'  data-tag='" + app_tag + "' >" +
        "<div class='game-box-link' data-id='" + game.ID + "' data-orientation='" + orientation + "' data-ajax='false' data-url='" + link + "' data-transition='none'>" +
        "<img src='" + thumb + "' class='game-thumb'>" +price+
        "</div></li>";
        }
    }
}

获取详细信息

function getDetails(productlist)
{
    inappbilling.getProductDetails(successHandler2, errorHandler, productlist);
}

successHandler2

function successHandler2 (result)
{
    price= result[0].price;
    console.log(price);
}

enter image description here

在图像中,您可以看到列出所有游戏后价格正在上涨。

javascript
3个回答
2
投票

我只是将您的代码重写为递归调用:

function render(res)
 {

    var length = res.length;
    var i=0;
    var price;

    function display(){
     var gameOnScreen = "<li class='game-box showlist'  data-tag='" + app_tag + "' >" +
            "<div class='game-box-link' data-id='" + game.ID + "' data-orientation='" + orientation + "' data-ajax='false' data-url='" + link + "' data-transition='none'>" +
            "<img src='" + thumb + "' class='game-thumb'>" +price+
            "</div></li>";
     i++;//incrementing to nextitem, but not triggering next iteration as a loop.
    };
    function getDetails()
    {
            if(res[i].product_id!='Free' && res[i].product_id!='')
            {
                inappbilling.getProductDetails(successHandler2, errorHandler, res[i].product_id);
            }
            else
            {
                price='Free';
                display();//displaying item
            }

    };
    function successHandler2 (result)
    {
        price= result[0].price;
        console.log(price);
        display();//displaying current item
        if(i<length){
            getDetails();//after getting current item's price go for next
        }
    };
    getDetails();//initial call

}

这将在完成当前项目后移至下一个项目。另请注意,我没有处理errorHandler


0
投票

像这样向getDetails()添加回调:

function getDetails(productlist, callback)
{
    inappbilling.getProductDetails(function successHandler2(result) {
        callback(result[0].price);
    }, errorHandler, productlist);
}

现在您可以使用致电getDetails()的价格:

getDetails(res.product_id, function callback(price) {
    // now you can update the DOM to show the actual price.
});

但是,我只会在获取价格后将其显示在列表中(将其添加到回调中的DOM中)。否则,它将在短时间内显示为免费。


0
投票

首先,确保呈现所有HTML,将产品ID用作类/ ID或HTML属性以区分不同的产品。

<li class='game-box showlist'  data-productid='productId' >
    ...
</li>

然后,调用getProductDetails,传入指向产品的HTML选择器。成功回调返回后,您可以使用它来更新HTML,如下所示:

function getDetails(productlist, selector)
{
    inappbilling.getProductDetails(function(result) {
        var price= result[0].price;
        $(selector).html(price);
    }, errorHandler, productlist);
}

注意,您可能希望使用其他占位符,而不是将默认价格设置为“免费”。]

© www.soinside.com 2019 - 2024. All rights reserved.