Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
evanritscher
10,209 PointsCannot understand async javascript
I am trying to record a browser's longitude and latitude with a lot of frustration.
I know that it is an asynchronous process but cannot seem to figure out how to handle that.
I can't see what I need to do to create the function that goes out and looks for the coordinates and then fills the global variables once it's received them.
if someone could walk me through this, I'd be eternally grateful.
Here is what I have so far and I am stuck:
var userLong;
var userLat;
$(document).ready(function(){
function currentLocal(coordinates){
if(navigator.geoLocation){
navigator.geolocation.getCurrentPosition(function(position){
userLat = position.coords.latitude;
userLong = position.coords.longitude;
});
}}
1 Answer
evanritscher
10,209 PointsSo I've somehow seemed to get something that works but I don't understand why, how or what it does. Could someone please walk me through what's going here:
var userLong;
var userLat;
$(document).ready(function(){
function currentLocal(callback){
navigator.geolocation.getCurrentPosition(
function(position){
userLat = position.coords.latitude;
userLong = position.coords.longitude;
callback();
})
}
function findLong(){
(userLong)
}
currentLocal(findLong)
})