Home > Net >  Using variable in another function Javascript
Using variable in another function Javascript

Time:11-05

Hello I have a problem with AJAX I got my data from the url but I want to use the variable IsochroneData into the function geo_json_add. How can I do this? I can console log the data that works fine. But getting the variable to the other function is my problem.

$.getJSON("/isochrone", function (data) {
 var isochroneData = data
 console.log(data);
 function geo_json_add(isochroneData) {
   geo_json
   .addData(isochroneData)
   .addTo(map);
 }
 geo_json_add(isochroneData);

CodePudding user response:

Fix your syntax and indentation

$.getJSON("/isochrone", function (data) {
 var isochroneData = data
 console.log(data);
 function geo_json_add(isochroneData) {
   geo_json
   .addData(isochroneData)
   .addTo(map);
 }
 geo_json_add(isochroneData);
})

if you want to use geo_json_add elsewhere, declare it ouside of the getJSON.

function geo_json_add(isochroneData) {
  geo_json
  .addData(isochroneData)
  .addTo(map);
}

$.getJSON("/isochrone", function (data) {
  var isochroneData = data
  console.log(data);

  geo_json_add(isochroneData);
})

CodePudding user response:

When you want to make a variable global, you've to declare it outside the function then set its value in the function, without the declaratory keyword then it'd be global.

var isochrone; - Outside the function
isochrone = data - In the function

  • Related