0

I have two functions:

function ajaxtakesource4(callback){
    var ajaxRequest;  // The variable that makes Ajax possible!
    try{
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                alert("Your browser broke!");
                return false;
            }
        }
    }   
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange =function(){
        if(ajaxRequest.readyState == 4 &&ajaxRequest.status==200){
            var sourcetest = ajaxRequest.responseText;  
            callback(sourcetest);
        }
    }
    ajaxRequest.open("POST", "takesource4.php", true);
    ajaxRequest.send(null); 
}

The second one, called ajaxtakesource3 is similar to ajaxtakesource4. Then i have

function run() {
    var somous4 , somous3 ;
    ajaxtakesource4(function(sourcetest){   
       var  somous4=sourcetest;
    });
     ajaxtakesource3(function(sourcetest){   
       var  somous3=sourcetest;
    });
    var result= soumous3+somous4;
    alert (result);
}

I call the above the function on button click:

<div id="run">
    <button id="button_run" class="button" onclick="run()">Run</button>
</div>

The problem is that i can use the variable somous4 only in the function ajaxtakesource4 and the same the somous3 variable. I would like to use them in run function.

skobaljic
  • 9,379
  • 1
  • 25
  • 51
SoMous
  • 29
  • 4
  • `var somous3` inside `function-block` will be local but this is not primary the issue..Basically, You need to test whether all the callbacks are invoked and then do the `sum` – Rayon Apr 11 '16 at 13:15
  • i found this question http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call and especially the answer of If you're not using jQuery in your code, this answer is for you - restructured code BUT there is not the answer in my question. So please when duplicate a question please modify the problem of the user or make it clear if there is the answer in this topic and where. – SoMous Apr 11 '16 at 13:19
  • The highest rated answer, which is the accepted answer, which has the accepted bounty on it answers this. It uses jQuery in the examples but the principles are the same. – Quentin Apr 11 '16 at 13:23
  • 1
    Obviously my question is without using Jquery so i dont think that your answer excuse the duplication. – SoMous Apr 11 '16 at 13:25
  • I would also like to see the answer in pure Javascript. – skobaljic Apr 11 '16 at 13:28
  • you haev right about the second comment and especially the paragraph Restructure code if you saw it and if you saw my code i dont have something similar to function myHandler(result) He wrote in this function that code that depends on `result and then he calls the function. When i tried to modify the run function it doesnt work. – SoMous Apr 11 '16 at 13:59

0 Answers0