javascript - Loop Calling Function in Native Ajax -


here's native ajax code:

            function script_grabbed(str) {                 var xmlhttp = new xmlhttprequest();                 xmlhttp.onreadystatechange = function() {                 if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {                     document.getelementbyid("numvalue").value = xmlhttp.responsetext;                     var result = document.getelementbyid("numvalue").value;                     if (typeof result !== 'undefined'){                                      alert('data found:' + result);                         //start: new request data #valdata                         xmlhttp.open("post", "inc.php?q="+str, true);                         document.getelementbyid("valdata").value = xmlhttp.responsetext;                         xmlhttp.send(null);                         var dataval = document.getelementbyid("valdata").value;                         if (typeof dataval !== 'undefined'){                                 alert('data bound:' + dataval);                             //continue call maps                             script_dkill()                             }                         //end: new request data #valdata                         }                     }                 }                 xmlhttp.open("post", "inc_num.php?q="+str, true);                 xmlhttp.send(null);             } 

from code, let me explain that:
want data/value result , dataval. after data, execute script_dkill() function.

however, creates loop , never script_dkill.
so, question is: how script_dkill , execute it?

for example: script_dkill() has content follow:

function script_dkill(){    alert('hallo, call me!'); } 

any help, please...

you need use different xmlhttprequest object second request, since using same object call same onreadystatechange event again , again

function script_grabbed(str) {   var xmlhttp = new xmlhttprequest();   xmlhttp.onreadystatechange = function() {     if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {       document.getelementbyid("numvalue").value = xmlhttp.responsetext;       var result = document.getelementbyid("numvalue").value;       if (typeof result !== 'undefined') {         alert('data found:' + result);         //start: new request data #valdata          var xmlhttp2 = new xmlhttprequest();         xmlhttp2.onreadystatechange = function() {           if (xmlhttp2.readystate == 4 && xmlhttp2.status == 200) {             document.getelementbyid("valdata").value = xmlhttp2.responsetext;             var dataval = document.getelementbyid("valdata").value;             if (typeof dataval !== 'undefined') {               alert('data bound:' + dataval);               //continue call maps               script_dkill()             }            }         }         xmlhttp2.open("post", "inc.php?q=" + str, true);         xmlhttp2.send(null);          //end: new request data #valdata       }     }   }   xmlhttp.open("post", "inc_num.php?q=" + str, true);   xmlhttp.send(null); } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -