HTML5顺序执行

时间:2010-08-18 01:17:43

标签: html5 storage sequential

我已经为产品详细信息(productId,name,quantityonhand)创建了一个Web存储,并从服务器中将记录填充到其中。我必须验证所需数量是否可用于接受订单。

产品表单包含用于Web存储中可用的每个产品的复选框(名称=“产品”)以及用于接受所需数量的相应文本输入字段。

验证方法定义如下。

function productValidation(){

    for(i=0;i<document.Products.product.length;i++){

        // checking whether the product has been checked / choosen

        if (document.Products.product[i].checked==true){
        var productId = document.Products.product[i].value;
            var qty = document.Products.p[i].value;
            var db = systemDB;

            // validating the available quantity 

            db.transaction(
                function(transaction){
                    transaction.executeSql('select * from product where productId=?;', 
                    [productId], 
                    function(transaction, results){
                        for (var j=0; j<results.rows.length; j++) {
                            var row = results.rows.item(j);
                            if (qty>row['qoh']){
                                alert(
                                    row['productname'] 
                                    + ' is out of stock. We can serve you only ' 
                                    + row['qoh'] + ' quantities currently!');                                    
                                document.Products.product[i].checked = false;                     
                                document.Products.p[i].value = 0;
                            }
                        }
                    });
                }
            );  
        }
    }
    return false;
}

当执行此代码时,由于db.transaction的异步性质,外部循环正在执行,并且仅在最后选择的产品上进行验证。

帮我解决这个问题。我希望按顺序执行。

Yuvi

1 个答案:

答案 0 :(得分:0)

尝试在db.transaction的回调函数中将调用链接在一起:

function productValidation(){

    checkProductsSequential(document.Products.product, 0);
    return false;
}

function checkProductsSequential(products, i)
{
    if (i < products.length)
    {
        // checking whether the product has been checked / choosen
        if (document.Products.product[i].checked==true){
            var productId = document.Products.product[i].value;
            var qty = document.Products.p[i].value;
            var db = systemDB;

            // validating the available quantity 

            db.transaction(
                function(transaction){
                    transaction.executeSql('select * from product where productId=?;', 
                    [productId], 
                    function(transaction, results){
                        for (var j=0; j<results.rows.length; j++) {
                            var row = results.rows.item(j);
                            if (qty>row['qoh']){
                                alert(
                                    row['productname'] 
                                    + ' is out of stock. We can serve you only ' 
                                    + row['qoh'] + ' quantities currently!');                                    
                                document.Products.product[i].checked = false;                     
                                document.Products.p[i].value = 0;
                            }
                        }
                        checkProductsSequential(products, i + 1)
                    });
                }
            );  
        }
        else
        {
            checkProductsSequential(products, i + 1)
        }
    }
}