javascript - AJAX acting in a multi threaded manner -


i understand javascript single threaded (as explained in question: if javascript not multithreaded, there reason implement asynchronous ajax queuing?), trying understand how applies application have developed. please see code below:

function getsqltable() {         var str = $("#<%=fielddatabasereferences.clientid%>")[0].value         var res = str.split(",");         $("#loadingimage").show();         $("#loadingimage2").show();          (var = 0; < res.length; i++) {             (function (i, res) {                 settimeout(function () {                     getsqltable2(i, res.length, res)                 }, 0);             })(i, res);         }     }  function getsqltable2(i,reslength,res) {             //if (i == 0)             //{             //    var start = new date().gettime();             //}         var div = document.createelement('div');             div.id = "div" +             document.getelementbyid('info_div').appendchild(div);              var possiblespage = false;             $.ajax({                 type: "post",                 url: "primarynominalajax.aspx/getsqltable",                 data: '{username: "' + $("#<%=fieldusername.clientid%>")[0].value + '", terminalname: "' + $("#<%=fieldterminalname.clientid%>")[0].value + '", terminalip: "' + $("#<%=fieldterminalip.clientid%>")[0].value + '", mappingid: "' + res[i] + '", usergroup: "' + $("#<%=fieldusergroup.clientid%>")[0].value + '", usn: "' + $("#<%=fieldusn.clientid%>")[0].value + '", requester: "' + $("#<%=fieldrequester.clientid%>")[0].value + '", reason: "' + $("#<%=fieldreason.clientid%>")[0].value + '", rrd: "' + $("#<%=fieldrrd.clientid%>")[0].value + '", review: "' + $("#<%=fieldreview.clientid%>")[0].value + '", possibles: "' + possiblespage + '",linkno: "", urn1: "", urn2: ""}',                 contenttype: "application/json; charset=utf-8",                 timeout: 80000000,                 datatype: "json",                 success: onsuccess(i, reslength),                 error: onerror,                 failure: function (response) {                     alert('there error loading webpage')                 }             });         } 

fielddatabasereferences populated on server side. ajax connects multiple local databases (up 30) , puts information on screen , when ready.

the calls various database servers asynchronous. surely has multi threaded effect?

javascript single threaded. when asynchronous events occur, are pushed queue waiting executed until thread idle. consider following example:

var run = true; var brk = date.now() + 5000;    // 5 seconds settimeout(function(){     run = false;                // set run variable false _asynchronously_ }, 1000);                       // after 1 second while(run && date.now() < brk); // loop while both conditions true console.log("run:", run);       // logs run: true (which initial value) 

when suppose loop terminate? 1 second? no run indefinitely (if date.now check not there). fact value logged in console true confirms timeout not fired. in queue, waiting var run = true...console.log() block terminate.


as example, order of execution be:

/* note: no 2 functions execute @ same time */ getsqltable(); /* functions scheduled via settimeout execute 1 one */ getsqltable2(0, ...); getsqltable2(1, ...); getsqltable2(2, ...); /* ajax requests complete 1 one, not in order started */ onsuccess(2); onsuccess(0); /* javascript thread idle during callbacks */ onsuccess(1); 

references:


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -