1

I am working in javascript. I have two functions that both compute a value. Each function also makes an ajax call to a service and grabs some data. Normally, the data is just returned in an object. Both of these functions I want to occur on a button click, so I've wrapped both of my "Compute Functions" in another function. The compute functions set values. Those values that are set, how do I use them in my wrapper function? This may be something simple that I'm just not getting

function ComputeSum() {
$.ajax({
    type: 'GET',
    dataType: 'json',
    url: constructedURL,
    success: 
    function(data) {
    callback(data);
    stopSpinner();
    var TheSum = 4+4;
    return TheSum;

},
error: function (xhr, status, error) {
    alert("Error - Something went wrong on the retrieval of already existing Hydraulic Data.");
    //alert("Unable to communicate with server.Status is: " + status + "The error is: " + error + "The xhr is: " + xhr);
    stopSpinner();
}
});

 }
function ComputeDIf() {$.ajax({
        type: 'GET',
        dataType: 'json',
        url: constructedURL,
        success: 
        function(data) {
        callback(data);
        stopSpinner();
  var TheDif = 10-2;

    return TheDif;

    },
    error: function (xhr, status, error) {
        alert("Error - Something went wrong on the retrieval of already existing Hydraulic Data.");
        //alert("Unable to communicate with server.Status is: " + status + "The error is: " + error + "The xhr is: " + xhr);
        stopSpinner();
    }
  });

}

So I have my two extra basic functions. I call these functions in another function thats attached to a button click

function Calculations() {
    ComputeSum();
    ComputeDif();
    alert("The sum is: " + TheSum);
    alert("The difference is: " + TheDif);
}

So my ajax call is returning an object but I also want to be able to use those values I created in the Compute Functions inside my wrapper function. Is this possible? What am I missing. Thanks in advance for your help.

user1898629
  • 329
  • 1
  • 4
  • 22
  • It is considered inappropriate here to post your question one way, then to edit it into a different question. This does a disservice to the people that provided answers to your first question, but now you've edited to a completely different question. If you have a new question, then you should post a new question, not change the old one. – jfriend00 Nov 09 '15 at 16:30
  • Now that you've changed your question into a question about return a value obtained from an ajax call, it is a duplicate. – jfriend00 Nov 09 '15 at 16:55

2 Answers2

1

Perhaps I'm not understanding the question, but why not use the return values like this:

function Calculations() {
    var TheSum, TheDif;
    TheSum = ComputeSum();
    TheDif = ComputeDif();
    alert("The sum is: " + TheSum);
    alert("The difference is: " + TheDif);
}

The variables TheSum and TheDif are local variable and limited to the scope of each function. The use of var assures this.

Karl
  • 1,814
  • 1
  • 25
  • 37
  • See, I'm such an idiot. That works perfectly. However, I actually excluded another part to the problem. I will update my question – user1898629 Nov 09 '15 at 16:23
0

You just assign the return values from the function to a local variable and then you can refer to that local variable within your function:

function Calculations() {
    var sum = ComputeSum();
    var dif = ComputeDif();
    alert("The sum is: " + sum);
    alert("The difference is: " + dif);
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Again, I just wasn't thinking. That also works. However, I actually excluded another part to the problem. I will update my question – user1898629 Nov 09 '15 at 16:23