简体   繁体   中英

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

I am building a phonegap application and my current native platform is android. I have a problem. I have a array list built in java. And I have to compare the array with a service that i called from 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.

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. 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. Therefore you should change your code such that you call the consecutive cordova.execs inside the callback itself or implement something similar; simply wait for each exec call to complete before calling the next one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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