javascript - Using XMLHttpRequest Timeout -


so trying use timeout property of xmlhttprequest "recover" program when request data times out. if fails in retrieving data, want try again. @ moment code looks (full url removed fit neatly):

    function pullrequest(){          var xhr = new xmlhttprequest()          xhr.onreadystatechange = function() {             if (this.readystate === 4) {                 jsondecode = this.responsetext;                 json = json.parse(jsondecode);                 ask = (json.result.ask);                 bid = (json.result.bid);             }         }          xhr.open("get","<url>",true);         xhr.send();  } 

i'm not totally following how implement timeout property, or if going want. did add following 2 lines after xhr.openbut threw , error:

        xhr.timeout = 5000;         xhr.ontimeout = pullrequest() 

basically in head if times out, run pullrequest function again. i'm sure not idea i'm not experienced enough know why. it's worth snippet of error follows:

...\node_modules\xmlhttprequest\lib\xmlhttprequest.js:165     settings = {     ^  rangeerror: maximum call stack size exceeded @ exports.xmlhttprequest.open  

any suggestions in how achieve goal, or point literature assist me appreciated.

thanks!

the problem you're calling pullrequest:

xhr.ontimeout = pullrequest() // ------------------------^^ 

since calls itself, calls again, , calls again, etc., runs out of stack when maximum recursion level of environment reached.

you don't want call there, want assign function reference ontimeout if timeout occurs, function gets called:

xhr.ontimeout = pullrequest // no () ------------------^ 

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 -