The third argument in xhr.open() is set to true, this means that the request is asynchronous... the function executes completely no matter if the answer was received or not. What you need to do is assign a receiving function with...
xhr.onreadystatechange = your_receiving_function;
Exemple with your code...
var xhr = null;
function checktimer() {
try {
xhr = new XMLHttpRequest()
if(xhr.overrideMimeType) {
xhr.overrideMimeType('text/html');
}
} catch(e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
if(xhr) {
userid = document.getElementById('this_userid').value
gameid = document.getElementById('this_gameid').value
url = "other/is_in_game.php?userid="+userid+"&gameid="+gameid
if(document.getElementById('not_my_go').value == 'true') {
xhr.onreadystatechange = checktimer_receiver;
xhr.open('GET',url,true);
xhr.send(null);
}
}
}
function checktimer_receiver() {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
var response = xhr.responseText;
if(response == "1") {
alert("The answer is 1 !");
} else {
alert("The answer isn't 1... let's try again !");
window.setTimeout("checktimer();", 3000);
}
} else {
alert("An error has occured. #" + xhr.status);
}
}
}
I didn't test it, so you might have some things to fix...
What is important :
-> Declare a global variable (here "xhr") that will be the AJAX object (declare it outside any function)
-> When creating the AJAX object, make sure to "overrideMimeType" with "text/html" if the object was created with XMLHttpRequest()...
-> Create a receving function that uses the globab variable ("xhr") and reads the response... I added a status verification, in case...
-> In your sending function, use "xhr.onreadystatechange" to assign the receiving function. DO NOT put (), just the name !
That's about it. Give it a try and give me some news..