I'm trying to wait for the AJAX request to plete. It would be easy if the method xmlhttp.open
would support async = false
but Ant Galio does not support this option and only asynchronous requests are permitted. The question is how can I wait for the callback to be called.
var ajaxFinished = false;
var xmlhttp = new XMLHttpRequest();
this.debug("-- onreadystatechange is being defined");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
ajaxFinished = true;
var data = xmlhttp.responseText;
if (xmlhttp.status == 200) {
that.debug('downloadSettings: SUCCESS');
[...]
} else {
that.debug('downloadSettings:');
that.debug('-- Error: ');
that.debug('-- ResponseText: "'+data+'"')
}
}
}
while (ajaxFinished == false) {
}
this.debug("-- open connection");
xmlhttp.open("GET", requestUrl, true); /* Ant Galio does not support synchronous */
this.debug("-- send");
xmlhttp.send();
I'm looking for some kind of active waiting. I know about another solution but I'm interested in a solution that would not require changing more of the code than is my example above.
Thanks!
I'm trying to wait for the AJAX request to plete. It would be easy if the method xmlhttp.open
would support async = false
but Ant Galio does not support this option and only asynchronous requests are permitted. The question is how can I wait for the callback to be called.
var ajaxFinished = false;
var xmlhttp = new XMLHttpRequest();
this.debug("-- onreadystatechange is being defined");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
ajaxFinished = true;
var data = xmlhttp.responseText;
if (xmlhttp.status == 200) {
that.debug('downloadSettings: SUCCESS');
[...]
} else {
that.debug('downloadSettings:');
that.debug('-- Error: ');
that.debug('-- ResponseText: "'+data+'"')
}
}
}
while (ajaxFinished == false) {
}
this.debug("-- open connection");
xmlhttp.open("GET", requestUrl, true); /* Ant Galio does not support synchronous */
this.debug("-- send");
xmlhttp.send();
I'm looking for some kind of active waiting. I know about another solution but I'm interested in a solution that would not require changing more of the code than is my example above.
Thanks!
Share Improve this question edited Aug 1, 2012 at 12:49 Bergi 665k161 gold badges1k silver badges1.5k bronze badges asked Aug 1, 2012 at 12:41 MartyIXMartyIX 28.7k32 gold badges139 silver badges217 bronze badges3 Answers
Reset to default 5yes, you can
function getFile(url) {
if (window.XMLHttpRequest) {
AJAX=new XMLHttpRequest();
} else {
AJAX=new ActiveXObject("Microsoft.XMLHTTP");
}
if (AJAX) {
AJAX.open("GET", url, false);
AJAX.send(null);
return AJAX.responseText;
} else {
return false;
}
}
var fileFromServer = getFile('http://somedomain./somefile.txt');
w3c definition http://www.w3/TR/XMLHttpRequest/#the-open()-method
client . open(method, url [, async = true [, user = null [, password = null]]])
You can't. There is no "active waiting" in JavaScript, there can be only one active execution a time ("single-threaded").
There is a workaround. Instead of using the the blocking while loop for poll use the nonblocking setInterval().. so your code might look something like this.
var ajaxFinished = false; var xmlhttp = new XMLHttpRequest(); this.debug("-- onreadystatechange is being defined"); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { ajaxFinished = true; var data = xmlhttp.responseText; if (xmlhttp.status == 200) { that.debug('downloadSettings: SUCCESS'); [...] } else { that.debug('downloadSettings:'); that.debug('-- Error: "); that.debug('-- ResponseText: "'+data+'"') } } } //Polling function function checkEvent(){ if(ajaxFinished == true){ //your code i.e xmlhttp.open("GET", requestUrl, true); } clearInterval(chkeventid);//Clear Interval via ID for single time execution } var chkeventid=self.setInterval("checkEvent()",100);//The poll call
The setInterval method is treated a bit differently in JS as you know so you may use it as against the while loop.