-1

Hi I am new in java script ,How I can use instead of xmlhttp.onreadystatechange=function() check the state of function, xmlhttp.readyState ,every 10 milliseconds, with setTimeout ,i don't know how to do it every 10 milliseconds.

thank you.

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
user1059769
  • 43
  • 2
  • 10
  • 1
    I would be more interested in knowing WHY would you want to do it? – DarthJDG Nov 19 '13 at 13:00
  • Becouse on some computers my application doesn't work ,it doesn't save the changes and I red that it might be a problem,xmlhttp.onreadystatechange. – user1059769 Nov 20 '13 at 06:52

2 Answers2

1

xmlhttp.onreadystatechange will be called whenever it changes state.

You don't need a setTimeout to check the value of xmlhttp.readyState.

xmlhttp.onreadystatechange = function () {
    console.log("readyState is now: " + xmlhttp.readyState);
}

You could do something like:

var state = 0;
xmlhttp.onreadystatechange = function () {
    state = xmlhttp.readyState;
}
setInterval(function () {
    console.log("readyState is now: " + state );
}, 100); // prints the readystate every 100ms

But I don't see why you'd need to do it like this.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

This will function will continually recurse every 10ms:

(function readyStateCheck(){

        //code to do the ready-state check

        if  (!someBreakingCondition){

            setTimeout(readyStateCheck, 10);

        }

})()
Faust
  • 15,130
  • 9
  • 54
  • 111
  • 1
    Don't forget some `if` logic to finally end this loop, too! – Paul S. Nov 19 '13 at 12:49
  • 1
    Frits answer (+1) probably makes this OBE. But one note: there are subtle differences between calling `setTimeout`, and `setInterval`. See this answer: http://stackoverflow.com/a/731625/613004 – Faust Nov 19 '13 at 12:54
  • Hi I found what the problem I think my javascript doesn't work with IE9+,so I Don't know How to fix the xmlhttp. – user1059769 Nov 20 '13 at 10:58