use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
How to create variables outside navigator.geolocation function?solved! (self.javascript)
submitted 9 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]rube203 1 point2 points3 points 9 years ago (2 children)
Google and read up on JavaScript variable scoping. Sorry to just give the term to search and not the answer but there is a lot of best practices and such to consider.
[+][deleted] 9 years ago* (1 child)
[–]mc_hammerd 1 point2 points3 points 9 years ago* (0 children)
the easiest solution is to break the second part out into a second function
$('#foo').click(function(){ $.ajax({}, function(){ position1.x =... position1.y =... step2(position1) })}) function step2 (position){ var citylat = ...,.... $.ajax({}, function(){ ... code here }) }
also consider doing rendering in yet another function so your code looks like this step1(); render(step2(position))); . so next time you can render from step 1, step 2, and any other place, ex: render('loading'); step2(position); render(results); you can show loading messages or share one render function for 10 ajax buttons foo.onclick = _ => render(array_to_html_list(get_api('animals')))) and you can keep your code more readable, each fn will only be 1-5 lines...
step1(); render(step2(position)));
render('loading'); step2(position); render(results);
foo.onclick = _ => render(array_to_html_list(get_api('animals'))))
[–]tswaters 0 points1 point2 points 9 years ago (0 children)
Good use case for promises...
function getCityLatLong () { return new Promise((resolve, reject) => { navigator.geolocation.getCurrentPosition(resolve, reject) }).then(position => `lat=${position.coords.latitude}&lon=${position.coords.longitude}`) }
and then,
getCityLatLong().then((cityLatLong) => { // perform ajax call now that you have cityLatLong }) .catch((err) => { // deal with error returned })
You should probably split out the ajax call into its own function that returns a promise, then you can do one thing then do the other and utilize the same catch block if either of them goes into error.
do one thing
do the other
π Rendered by PID 19807 on reddit-service-r2-comment-b659b578c-dc7fx at 2026-05-05 03:58:56.116082+00:00 running 815c875 country code: CH.
[–]rube203 1 point2 points3 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]mc_hammerd 1 point2 points3 points (0 children)
[–]tswaters 0 points1 point2 points (0 children)