-4

I'm using this function in onclick:

function showUser(str) {
if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
} else {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("txtHint").innerHTML = this.responseText;
        }
    };
    xmlhttp.open("GET","reseller.php?ostan="+str,true);
    xmlhttp.send();
    get_lat_lon()
}
}

I need to call get_lat_lon() function right after previous works done, but get_lat_lon() starts it self before previous jobs complete.

I need to call get_lat_lon() without delay when i set 1s delay it works correctly.

Here is my get_lat_lon:

    function get_lat_lon(){
  var mla = 0;
  var mlo = 0;
  var mz = 0;
  var la = 0;
  var lo = 0;
  var na = 0;
  mla = document.getElementById('mla').value;
  mlo = document.getElementById('mlo').value;
  mz = document.getElementById('mz').value;
  la = document.getElementById('la').value;
  lo = document.getElementById('lo').value;
  na = document.getElementById('na').value;...

and this is my php output:

        <input type="hidden" class="mla" id="mla" value="<?php echo $s1; ?>" />
        <input type="text" class="mlo" id="mlo" value="<?php echo $s2; ?>" />
        <input type="hidden" class="mz" id="mz" value="<?php echo $zoom; ?>" />
        <input type="hidden" class="os" id="os" value="<?php echo $ostan; ?>" />
        <input type="hidden" class="la" id="la" value="<?php echo $lat; ?>" />
        <input type="hidden" class="lo" id="lo" value="<?php echo $lon; ?>" />
        <input type="hidden" class="na" id="na" value="<?php echo $name; ?>" />
Alireza Sabahi
  • 559
  • 1
  • 9
  • 30
  • 6
    just move it inside `onreadystatechange` – Federico klez Culloca Jan 24 '18 at 13:09
  • Your php has nothing to do with this. The problem is that AJAX call (as the first 'A' in AJAX implies) are asynchronous, so when you call `get_fb()` in your code, it's almost impossible for the AJAX call to have returned already. EDIT: I was responding to OP's comment about the presence of the `php` tag. – Federico klez Culloca Jan 24 '18 at 13:14
  • moving get_fb to onreadystatechange is not working – Alireza Sabahi Jan 24 '18 at 13:23
  • @AlirezaSabahi: Define "not working". If you want to perform some action after an asynchronous operation completes, that's exactly how you'd do it. – David Jan 24 '18 at 13:30
  • @AlirezaSabahi: Your recent edit still has the exact same problem as the original question. You're trying to invoke an operation *immediately* instead of in response to the asynchronous operation. Just move `get_fb();` to where you want to execute it. In this case it would be in `onreadystatechange` immediately after updating the page with `responseText`. – David Jan 24 '18 at 13:47
  • @David thanks , fixed – Alireza Sabahi Jan 24 '18 at 13:51

2 Answers2

2

You should call it like this:

 xmlhttp.onreadystatechange = function() {
     if (this.readyState == 4 && this.status == 200) {
         document.getElementById("txtHint").innerHTML = this.responseText;
         get_fb();//  THIS
      }
   };
Amr Berag
  • 1,022
  • 7
  • 14
-2

JavaScript implements an async function declaration. What you'd be looking for is something like this:

function showUser(user)
{
    // your logic
    get_fb_async();
}

async function get_fb_async()
{
    get_fb() = await showUser(user);
}

This will have to vary according to how your get_fb() function behaves, but now you have the logic.

Further details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

TheLebDev
  • 469
  • 4
  • 17