javascript - Repeat XHR until a certain response code is received -


i'm rather new js , i'm trying repeat xhr varying header, until http response code received. far i've come following code. did not write createcorsrequest function, i've copied other stackoverflow questions.

<script> function createcorsrequest(method, url){     var xhr = new xmlhttprequest();     if ("withcredentials" in xhr){         xhr.open(method, url, true);     } else if (typeof xdomainrequest != "undefined"){         xhr = new xdomainrequest();         xhr.open(method, url);     } else {         xhr = null;     }     return xhr;   }  (i = 1; < 10; i++){     var url = "https://127.0.0.1/"     var xhr = createcorsrequest('put', url);     xhr.withcredentials = true;     xhr.setrequestheader('content-type', "application/json");     xhr.setrequestheader('x-counter', i);     xhr.send("{ \"body\": \"data\"}")      var xhr_status = xhr.status     console.log("log: "+i+" status: "+xhr_status)     if (xhr_status == 200){ break; } } </script> 

my problem is, xhr.status 0. assume has javascript being asynchronous - i'm not sure.

how can check xhr status after each request?

edit:

the settimeout approach seems viable solution, i'm new js, don't quite understand jquery yet. able explaining how can used in code snippet?


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -