-5

I have ajax in those highlighted functions, java script dose not wait until the request comes from the server goes ahead and execute the other commands I don't won't that!

I am a newbie to java script...

here code

            function putthedataback() {
                for (var i = 0; i < year.length; i++) {
                    var a = i + 1;
                    year1fun(year[i],a);
                    cc1fun(cc[i],a);
                    tp1fun(tp[i],a);
                    document.getElementById("year" + a).value = "" + year[i];
                    document.getElementById("cc" + a).value = "" + cc[i];
                    document.getElementById("tp" + a).value = "" + tp[i];
                    document.getElementById("du" + a).value = "" + du[i];
                    document.getElementById("dd" + a).value = "" + dd[i];
                    document.getElementById("ns" + a).value = "" + ns[i];
                    document.getElementById("fv" + a).value = "" + fv[i];
                   
                }
            }

ajax one function

code

function year1fun(value, x) {
                        var xmlhttp = new XMLHttpRequest();
                        xmlhttp.onreadystatechange = function () {
                            if (this.readyState === 4 && this.status === 200) {
                                document.getElementById("cc" + x).innerHTML = this.responseText;
                            }
                        };
                        xmlhttp.open("GET", "yearselectionchanged.php?q=" + value, true);
                        xmlhttp.send();
                    }

here a ajax containing function

Avon97
  • 47
  • 12
  • 2
    Put anything you want to happen _after_ the request in the callback. I'm sure this _has_ to be a duplicate of at least a handful of questions. – Patrick Q Apr 11 '18 at 19:34
  • Duplicate: https://stackoverflow.com/questions/5485495/how-can-i-take-advantage-of-callback-functions-for-asynchronous-xmlhttprequest – Kevin Raoofi Apr 11 '18 at 19:38
  • @PatrickQ then i will lose the for loop thankz for reply – Avon97 Apr 11 '18 at 19:39
  • Looks like you need to setup a promise chain. Which is fairly complex javascript, and requires a great deal of prior head-wrapping-aroundis first. .... or restructure and nest the dom manipulation inside each ajax success.... either way, you got some work ahead of you ;) – IncredibleHat Apr 11 '18 at 19:47
  • Not quite exact duplicates (I don't think) but you may want to look at [this](https://stackoverflow.com/questions/2687679/jquery-ajax-inside-a-loop-problem) and [this](https://stackoverflow.com/questions/19265111/how-to-put-ajax-inside-a-loop) and generally other search results for "ajax within loop" – Patrick Q Apr 11 '18 at 19:51

1 Answers1

-1

You has called xmlhttp.open("GET", "yearselectionchanged.php?q=" + value, true);

But the last param would be false, like this:

xmlhttp.open("GET", "yearselectionchanged.php?q=" + value, false);

True is a async request, then your code does not wait for response. And False is sync, your code will wait for response.

See the documentation here: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests#Synchronous_request

  • 1
    No, no, a million times no. "Synchronous XHR is now in deprecation state. Developers are recommended to move away from the API." – Patrick Q Apr 11 '18 at 19:45
  • @AvonPubuduJayaweera PLEASE DO NOT DO THIS! I cannot stress enough how bad an idea this is. – Patrick Q Apr 11 '18 at 19:56
  • @AvonPubuduJayaweera Because, as I quoted in my comment above, _the docs themselves tell you not to_. It defeats the whole purpose of ajax (the "a" means "asynchronous"). Any _single_ request that takes a while now forces _everything_ to wait for it. – Patrick Q Apr 11 '18 at 20:02
  • @PatrickQ hmm i got what you mean it's ok beacuse my project run on lan network not in web thankz for replying – Avon97 Apr 11 '18 at 20:06