有没有办法在javascript中执行阻止代码?

时间:2015-05-28 06:08:37

标签: javascript worklight-adapters

具体来说,我正在调用检索聚合值的数据库。然后将其显示给用户。但是在适配器调用之前(因为我正在使用worklight)可以完成,之后的代码会被执行,这是我想要阻止的。我已经尝试了从setTimeout到调用执行空while循环的函数的所有内容。它们似乎都没有工作,因为代码也正在跳过所有这些。有人可以帮我解决这个问题吗?

示例代码:

var flow_completed_percentage=0;
function setFlowStatus()
{
    var flow_name,flow_status_value;

    for(var i = 0;i < 2; i++)
    {
        flow_name=flow_name_DB[i].trim();//flow_name_DB is an array that stores the flow names
        flow_status_value=flow_status_DB[i].trim();//flow_status_DB is an array that stores the flow status values

        if(flow_status_value=="R" || flow_status_value=="A")
        {
            var invocationData_totalPercentage = {
                adapter : "dummy_adapter",
                procedure : "getJobPercentage",
                parameters : [flow_name]
            };

            WL.Client.invokeProcedure(invocationData_totalPercentage, {
            onSuccess : setPercentageSuccess,
            onFailure : setPercentageFailure
            });
        }
      }//end of for loop
     //the problem is next iteration of for loop begins before the adapter call for the current iteration completes.
     //So flow_name gets overwritten and it gives the wrong result.

    function setPercentageSuccess(total_percentage)
    {
      var tot_percent = total_percentage.invocationResult.resultSet[0];
      flow_completed_percentage=tot_percent.TOT_PERCENT;
      if(flow_status=="R")
        runningStatus(flow_name);
      else if(flow_status=="A")
        abortedStatus(flow_name);
    }

    function setPercentageFailure()
    {
       alert("Failed to fetch records from DB");
    }
}

function runningStatus(flow)
{
  //do something
}

function abortedStatus(flow)
{
  //do something
}

1 个答案:

答案 0 :(得分:0)

在完成数据库操作后,您必须调用剩余的代码块。

由于必须执行相同的操作次数,您可以多次调用该函数,而不是使用循环。

我尝试按如下方式更改代码:在完成数据库操作后,您可以使用下一个值i调用该函数。

var flow_completed_percentage=0;
function setFlowStatus()
{
    var flow_name,flow_status_value;
    var initial = 0;
    iterator(initial); // initial call

    function iterator(i)
    {
        flow_name=flow_name_DB[i].trim();//flow_name_DB is an array that stores the flow names
        flow_status_value=flow_status_DB[i].trim();//flow_status_DB is an array that stores the flow status values

        if(flow_status_value=="R" || flow_status_value=="A")
        {
            var invocationData_totalPercentage = {
                adapter : "dummy_adapter",
                procedure : "getJobPercentage",
                parameters : [flow_name]
            };

            WL.Client.invokeProcedure(invocationData_totalPercentage, {
            onSuccess : function() {
                setPercentageSuccess();
                iterateNext(i); // call next after DB completion
                },
            onFailure : function() {
                setPercentageFailure();
                iterateNext(i); // call next after DB completion
                }           
            });
        }
    }

    function iterateNext(current)
    {
        current= current+1;
        if(current<2){ // check the loop condition
            iterator(current);
        }
    }

    function setPercentageSuccess(total_percentage)
    {
      var tot_percent = total_percentage.invocationResult.resultSet[0];
      flow_completed_percentage=tot_percent.TOT_PERCENT;
      if(flow_status=="R")
        runningStatus(flow_name);
      else if(flow_status=="A")
        abortedStatus(flow_name);
    }

    function setPercentageFailure()
    {
       alert("Failed to fetch records from DB");
    }
}

function runningStatus(flow)
{
  //do something
}

function abortedStatus(flow)
{
  //do something
}