-1

I have the following function:

function charts(a, b, c){

//get the variables of function values and do some stuff with it

}

I get variables through a, b, c already from another function start:

function start(){
    var a = 1;
    var b = 2;
    var c = 3;
 charts(a, b, c);
 }

How can I use the variables of this function inside my function charts:

function values() {

    $.ajax({
        url: site_url + "/?key=XXX&email=" + email + "&values=yes" + '&callback=?',
        type: "GET",
        dataType: 'json',
        success: function (data) {
            value1 = data.value1;
            value2 = data.value2;
            value3 = data.value3;
        }
    });
}
desmeit
  • 550
  • 1
  • 7
  • 24

1 Answers1

2
function charts(a, b, c){
  //get the variables of function values and do some stuff with it
}

function values(){

  $.ajax({
    url: site_url + "/?key=XXX&email=" + email + "&values=yes" + '&callback=?',
    type: "GET",
    dataType: 'json',
    success: function(data){
      value1 = data.value1;
      value2 = data.value2;
      value3 = data.value3;
      charts(value1, value2, value3);
    }
  });
}

Edit:

If you want to pass values to charts and then call the ajax request and use a, b and c in the callback you try something like that:

function charts(a, b, c) {
  return function () {
    $.ajax({
      url: site_url + "/?key=XXX&email=" + email + "&values=yes" + '&callback=?',
      type: "GET",
      dataType: 'json',
      success: function (data) {
        value1 = data.value1;
        value2 = data.value2;
        value3 = data.value3;
        console.log(a);
        console.log(b);
        console.log(c);
      }
    });
  }
}

Usage:

var chartsFunction = charts(1,2,3);
chartsFunction();

Output:

1
2
3
Mihail Petkov
  • 1,535
  • 11
  • 11