accessing variable value outside of function

sanfranciscoave

I have 2 functions in my script. The purpose of function #1 is to produce a url called "mapsURL"

The purpose of the second function is to run an .ajax request with "mapsURL"

However I am having issues with accessing "mapsURL" in the second function. I declared "mapsURL" in the first function without a "var" in fornt of it. From my understanding, this should make the this the value global and I should be able to access it form inside other functions. Is my understanding incorrect or what am I missing here?

here's my js:

note: I removed my API key for this post, so that's not the issue

$(document).ready(function (){


  navigator.geolocation.getCurrentPosition(function (position) {
     var positionLat = position.coords.latitude
     var positionLon = position.coords.longitude
     mapsURL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + positionLat + "," + positionLon + "&key=***mykeygoeshere***";
  });


 function getData (){
   $.ajax({
     url: mapsURL,
     dataType: 'json',
     success: function(response){
      console.log(response);
     }
    });
  };

  getData();

});

CertainPerformance

getCurrentPosition is asynchronous. It doesn't assign to mapsURL immediately, so when you call getData synchronously, mapsURL hasn't been populated yet.

You should call getData inside the getCurrentPosition callback - which will also allow you to avoid using a global variable:

$(document).ready(function() {
  navigator.geolocation.getCurrentPosition(function(position) {
    var positionLat = position.coords.latitude
    var positionLon = position.coords.longitude
    var mapsURL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + positionLat + "," + positionLon + "&key=***mykeygoeshere***";
    getData(mapsURL);
  });
  function getData(mapsURL) {
    $.ajax({
      url: mapsURL,
      dataType: 'json',
      success: function(response) {
        console.log(response);
      }
    });
  }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing a variable that is outside an anonymous function

From Dev

Accessing a local variable outside a function

From Dev

Accessing a variable outside a given function

From Dev

Accessing a variable from outside the function?

From Dev

Scala accessing variable outside function

From Dev

Accessing variable outside redis pipelining function on Laravel

From Dev

accessing a variable from outside the function in python

From Dev

accessing a variable from outside the function in python

From Dev

Variable not retaining value outside of function

From Dev

Retrieve value of variable outside function

From Dev

accessing a variable from outside a function and returning the variable. In a module pattern

From Dev

Accessing a value from outside a function: js and php generated value

From Dev

Setting a value in a function as a variable outside the function (Swift)

From Dev

XSLT Accessing value of element from another variable outside a for-each

From Dev

How to get variable value outside a function IN jquery

From Dev

JQuery change variable value outside the function

From Dev

How to assign value to variable outside the function in $on AngularJS?

From Dev

How to get variable value outside angular function

From Dev

Accessing a variable that's outside of a script

From Dev

Accessing variable outside foreach loop

From Dev

Accessing variable outside foreach loop

From Dev

Javascript constructor: Accessing outside variable

From Dev

Accessing a variable outside a loop Python

From Dev

accessing variable outside the with keyword in python

From Dev

Accessing variables outside a function in Python

From Dev

Accessing Variables outside of a function in swift

From Dev

Accessing variables outside of a class and function

From Dev

Accessing a static function outside the file

From Dev

Accessing outside variables inside the 'then' function

Related Related

  1. 1

    Accessing a variable that is outside an anonymous function

  2. 2

    Accessing a local variable outside a function

  3. 3

    Accessing a variable outside a given function

  4. 4

    Accessing a variable from outside the function?

  5. 5

    Scala accessing variable outside function

  6. 6

    Accessing variable outside redis pipelining function on Laravel

  7. 7

    accessing a variable from outside the function in python

  8. 8

    accessing a variable from outside the function in python

  9. 9

    Variable not retaining value outside of function

  10. 10

    Retrieve value of variable outside function

  11. 11

    accessing a variable from outside a function and returning the variable. In a module pattern

  12. 12

    Accessing a value from outside a function: js and php generated value

  13. 13

    Setting a value in a function as a variable outside the function (Swift)

  14. 14

    XSLT Accessing value of element from another variable outside a for-each

  15. 15

    How to get variable value outside a function IN jquery

  16. 16

    JQuery change variable value outside the function

  17. 17

    How to assign value to variable outside the function in $on AngularJS?

  18. 18

    How to get variable value outside angular function

  19. 19

    Accessing a variable that's outside of a script

  20. 20

    Accessing variable outside foreach loop

  21. 21

    Accessing variable outside foreach loop

  22. 22

    Javascript constructor: Accessing outside variable

  23. 23

    Accessing a variable outside a loop Python

  24. 24

    accessing variable outside the with keyword in python

  25. 25

    Accessing variables outside a function in Python

  26. 26

    Accessing Variables outside of a function in swift

  27. 27

    Accessing variables outside of a class and function

  28. 28

    Accessing a static function outside the file

  29. 29

    Accessing outside variables inside the 'then' function

HotTag

Archive