简体   繁体   English

所有cordova.exec函数结束后,将执行成功回调

[英]Success callback is executing after all cordova.exec functions have ended

I am building a phonegap application and my current native platform is android. 我正在构建一个phonegap应用程序,而当前的本机平台是android。 I have a problem. 我有个问题。 I have a array list built in java. 我有一个内置在Java中的数组列表。 And I have to compare the array with a service that i called from javascript. 而且我必须将数组与我从javascript调用的服务进行比较。 But issue is that when i call cordova.exec inside for loop, first entire for loop executes and then the control shifts to callbacksuccess for all cordova.exec. 但是问题是,当我在for循环内调用cordova.exec时,首先执行整个for循环,然后控制权移至所有cordova.exec的callbacksuccess。

My code is- 我的代码是-

 $.ajax({
      url : ...,  
      type: "GET",
      data: null,
      setTimeout:1,
      dataType:"JSON",
      success: function(response)
      {
         var mydata='';
         for(var i=0;i<response.length;i++)
         {
           alert('inside for '+i);
            obj=response[i];
            var testpackage=obj.PackageName;
            cordova.exec(callbacks,callbacke,'MyPlugin','plugin2',[testpackage]);
         }
      },
     error: function()
     {
        alert('Failed to fetch list.Try again later.');
     }
    });
function callbacks(e)
{
alert('success');
}

My callbacks() function is called after the whole for loop executes. 在整个for循环执行之后,将调用我的callbacks()函数。 The output that I am receiving is- 我收到的输出是-

inside for 0
inside for 1
inside for 2
success
success
success

My expected output is- 我的预期输出是-

inside for 0
success
inside for 1
success
inside for 2
success

I have tried many things. 我已经尝试了很多东西。 Still I am not finding the solution. 我仍然没有找到解决方案。 Thanx in advance. 提前感谢。

cordova.exec call is asynchronous. cordova.exec调用是异步的。 Therefore you should change your code such that you call the consecutive cordova.execs inside the callback itself or implement something similar; 因此,您应该更改代码,以便在回调本身内部调用连续的cordova.execs或实现类似的东西; simply wait for each exec call to complete before calling the next one. 只需等待每个exec调用完成,然后再调用下一个。

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

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