0

I've this function.

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); 
}

Also:

var somous4;
function run() {
    ajaxtakesource4(function(sourcetest){   
        somous4=sourcetest;
    });
    alert(somous4);
}

and here I call the above the function:

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

When I click on the button it's supposed to alert response from Ajax request, but looks to be alerting a falsy value (undefined), as seen in this line:

alert(somous4);
SoMous
  • 29
  • 4
  • it's not clear what the questions is – llamerr Apr 07 '16 at 10:44
  • `The problem is tha the first time i press the button run i receive the alert undefine` , `It could be normal since the declaration of variable is before the function` so is it normal or not? – llamerr Apr 07 '16 at 10:45
  • what you trying to achive and what exactly isn't working as you expected? – llamerr Apr 07 '16 at 10:46
  • 1
    your alert statement should be inside callback function, as you are making an async call when you click the button first time the callback function would not have called yet and somous4=sourcetest wouldn't have executed yet. – Dhananjaya Kuppu Apr 07 '16 at 10:48
  • yes its true , but in fact i dont want to use alert i used it for my example. I need to use the variable somous4 or sourcetest in the run function. – SoMous Apr 07 '16 at 12:06
  • To be more clear i want to use many variables like somous4 so i need the variables in function run and not inside the ajaxtakesource4 – SoMous Apr 07 '16 at 12:23
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – David Apr 11 '16 at 13:45

3 Answers3

0

Asynchronous code executes concurrently in nature. Thus, your alert statement may execute before your callback executes (callback will only after it receives back data from server). Put the alert inside the callback and it will show the value returned i.e.

var somous4;
function run() {
    ajaxtakesource4(function(sourcetest){   
        somous4=sourcetest;
        alert(somous4);
    });
}

Edit: Based on OP's comment, instead of thinking about return values, do this:

function foo(soumous4) { // use somous4 for whatever you want... }

// Call this function foo inside the callback.
ajaxtakesource4(function(sourcetest){   
        somous4=sourcetest;
        foo(somous4);
    });
a_pradhan
  • 3,285
  • 1
  • 18
  • 23
  • yes its true , but in fact i dont want to use alert i used it for my example. I need to use the variable somous4 or sourcetest in the run function. – SoMous Apr 07 '16 at 12:03
  • To be more clear i want to use many variables like somous4 so i need the variables in function run and not inside the ajaxtakesource4 – SoMous Apr 07 '16 at 12:23
  • Anything which uses the variable after the asynchronous update needs to be called within the callback. So make a function and call that in the callback. – a_pradhan Apr 11 '16 at 07:27
  • in function run i would like to receive a number of variables like soumous3 , which i would like to receive from callback . Then i would like to run the algorithm according to these variables. The problem is that i will receive each variable form a different function. – SoMous Apr 11 '16 at 08:45
  • Then write a function which will only be called after all the variables are received. Also, if possible, send all data within a single ajax request. Look at JS/ES6 promises. – a_pradhan Apr 11 '16 at 11:23
  • Thanks for your interest. Firstly i dont want to alert the value in the calback function because in run function i need to use , modify a number of values like somous4 and to run the algorithm. I tried your code like function run(somous4) { ajaxtakesource4(function(sourcetest){ var source4 =sourcetest; run(somous4); }); alert(somous4); } – SoMous Apr 11 '16 at 12:37
  • you have right it is better to receive all the variables and then to write a funtion to run the algorithm . This was my first thought but the problem is that i am not able to store the variables so i cannot use them in the following function – SoMous Apr 11 '16 at 13:04
0

I suggest you change the callback in the run function as follows:

var somous4;
function run() {
    ajaxtakesource4(function(sourcetest){   
        somous4=sourcetest;
        alert(somous4);
    });
}
chriswirz
  • 278
  • 1
  • 9
  • yes its true , but in fact i dont want to use alert i used it for my example. I need to use the variable somous4 or sourcetest in the run function. – SoMous Apr 07 '16 at 12:03
  • To be more clear i want to use many variables like somous4 so i need the variables in function run and not inside the ajaxtakesource4 – SoMous Apr 07 '16 at 12:23
0

You're alerting somous4 before it's changed by the request callback. In this case the commands block executes first than the request callback.

Server-side languages as PHP does the work automatically, so you don't need to use events there. It sleeps while the request is not done. That's because the commands block turns the event callback.

  • thanks for your reply , i understood it but the problem is that i am not able to use the variable sourcetest or somous4 it the function run – SoMous Apr 07 '16 at 12:05
  • To be more clear i want to use many variables like somous4 so i need the variables in function run and not inside the ajaxtakesource4 – SoMous Apr 07 '16 at 12:24
  • @SoMous Then return the variable value in somewhere of callback function commands block. –  Apr 07 '16 at 12:31
  • You can't use `sourcetest` parameter of the anonymous callback function outside itself, it's a part from the function scope. –  Apr 07 '16 at 12:38
  • if you mean function run() { ajaxtakesource4(function(sourcetest){ somous4=sourcetest; return somous4 ; }); somous= ajaxtakesource4(); alert (somous); It doesn t work – SoMous Apr 07 '16 at 12:46
  • @SoMous That's wrong. I didn't mean that. And why are you returning value in the callback anonymous function? –  Apr 07 '16 at 12:48
  • you told me before " Then return the variable value in somewhere of callback function commands block. " – SoMous Apr 07 '16 at 12:54
  • i didnt understand what you mean , could you give an example? – SoMous Apr 07 '16 at 12:54
  • @SoMous No, I told you to use it to assign some identifier value or expression `identifier=sourcetest`, i.e. –  Apr 07 '16 at 12:54