-1

I tend to run into this problem quite often, and my solutions never really work the way i wanted to. Basically, I want one function to wait till the one before is completely finished running.
For example, I have a gps locating function, and i want another function to run once the gps one completes its tasks. I am not putting any code for this because i would like to see how people do it without seeing my mine, as to be completely objective.

I am using phonegap, jquery-mobile and mkaing hybrid apps.

z.a.
  • 2,549
  • 3
  • 12
  • 16

2 Answers2

0

Why can't you just do this ?

func1();
func2();

If it is more complicated than that, perhaps you messed up somewhere earlier in the problem.

Ben
  • 683
  • 3
  • 5
0

You can use $.Deferred:

function locate() { 
   var d = $.Deferred();
   locationService.start().then(function(position) {
       d.resolve(position);
   });
   return d;
}

locate().then(function(position) {
    console.log('Latitude: ' + position.Latitude + ' - Longitude: ' + position.Longitude);
});

jQuery Api docs http://api.jquery.com/jQuery.Deferred/

edotassi
  • 476
  • 2
  • 6
  • 19