Returning A Value Of Callback Function?
I'm having some trouble with function callbacks, specifically with the HTML5 geolocation API. I'm a little new to Js as well. The function: function getInfo() { navigator.geolo
Solution 1:
- To pass the callback, use just
getPos
instead of calling the function right away. - Use proper syntax to declare properties in
PositionObj
.
PositionObj = {
lat: null,
lon: null,
}
Solution 2:
Your PositionObj
literal is incorrect. the initial properties should be set with colons:
{ lat: '', lon: '' }
Solution 3:
The callback should tell you that the request to get position information is asynchronous. This means that getInfo()
does NOT set the values instantly, and therefore the following line cannot access the "new" values.
Anything that relies on the result of an asynchronous function MUST be within the callback itself.
Post a Comment for "Returning A Value Of Callback Function?"