Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript assigning the return value of a Callback function to global variable

My question is about Javascript. I have a Callback function which receives a Position object on a successful callback.

The problem is that when I try to set the properties of the Position object to a global variable at a successful callback it just does not let me do it and the global just remains undefined.

As a workaround to that instead of directly setting the object properties to global variables i'm trying to return it through the callback function but I couldn't find a way to set the return value of the callback function to a global variable.

Here's the simplified code.

var x;

navigator.geolocation.getCurrentPosition(onSuccess, onError);

//on Successful callback receives a Position Object
function onSuccess(position) {  
  var coords = position.coords;  
    x=coords;       // Setting directly to an object does not work x still remains undefined after succesful callback               
return coord; // Trying to set this to a global object 

}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}
like image 564
Serdar Dogruyol Avatar asked Aug 25 '11 11:08

Serdar Dogruyol


2 Answers

You can't return a value from the callback (in this case). That would mean that inside of getCurrentPosition, the return value from the callback has to be assigned somewhere.

Assigning it to a global variable works, but at the time you access that variable, it was not assigned the new value yet. E.g.

// x is assigned in `onSuccess`
var x;
navigator.geolocation.getCurrentPosition(onSuccess, onError);
alert(x); // will be undefined, the response is not processed yet

Think about it: getCurrentPosition is probably doing an Ajax request to get the position. Such a request takes (a) time (couple of milliseconds) and because of that (b) is asynchronous, which means that JavaScript does not wait until the response is received. Your code is way faster. onSuccess was not called yet when you alert x.

Only solution:

All the code that has to access the position as to be in or called from the callback.

like image 172
Felix Kling Avatar answered Sep 25 '22 15:09

Felix Kling


As described by Felix King, you cannot return a value from a callback and in your global variable variant, the variable will not be set until after the AJAX request completes and calls the callback (hence its name !).

Using a global variable you can solve your problem if you have some sort of processing loop, you can add this code to that loop to do what is required whenever the coord changes.

  if (coord) // global variable has a new value
    // process it
     alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
    // then clear it
    coord = null;
  } 
like image 22
HBP Avatar answered Sep 21 '22 15:09

HBP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!