nixoid
nixoid

Reputation: 25

How to make javascript wait until it gets a valid response from synchronous http request?

I know it is not a good idea to use synchronous requests but this is the case that I really need it.

I have tried to make getEndDate function calling to itself if response lenght is less than 20 but after the first unsuccessfull request (if url gives too short response) it goes to alert(enddate.EDDAYOW); and I am getting error, and getEndDate continues sending request every 500 ms.

I need getEndDate function to continue sending a request until it get a valid response and return valid object, and only after that continue to the next line of JS. How to achieve that?

var url = 'http://local.com/cgi-bin/hello2.pl';
// url returns a plain text:
// 1234567890 2013 05 May Friday 13 23 45 01


var enddate = getEndDate(url);

alert(enddate.EDDAYOW);

function getEndDate(url) {
    var xmlhttp = getXmlHttp();
    xmlhttp.open('GET', url, false);
    xmlhttp.send();
    if (xmlhttp.status == 200 && xmlhttp.responseText.length > 20) {
        var n = xmlhttp.responseText.split(" ");
        return {
            'edseconds': n[0],
            'EDYEAR': n[1],
            'EDMON': n[2],
            'EDMONNAME': n[3],
            'EDDAYOW': n[4],
            'EDDAY': n[5],
            'EDHOUR': n[6],
            'EDMIN': n[7],
            'EDSEC': n[8]
        };
    } else {
        setTimeout("getEndDate(" + url + ")", 500);
    }
}


function getXmlHttp() {
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

Upvotes: 1

Views: 4637

Answers (1)

mask8
mask8

Reputation: 3638

I don't know if this satisfies your requirement, but I would rewrite it to something like this:

var enddate;

getEndDate(url);

function do_rest(returnDate)
{
    enddate = returnDate;
    alert(enddate.EDDAYOW);

    // do more if you need
};


function getEndDate(url) {
    var xmlhttp = getXmlHttp();

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200 && xmlhttp.responseText.length > 20) {
                var n = xmlhttp.responseText.split(" ");
                do_rest({
                    'edseconds': n[0],
                    'EDYEAR': n[1],
                    'EDMON': n[2],
                    'EDMONNAME': n[3],
                    'EDDAYOW': n[4],
                    'EDDAY': n[5],
                    'EDHOUR': n[6],
                    'EDMIN': n[7],
                    'EDSEC': n[8]
                });
            } else {
                setTimeout("getEndDate(" + url + ")", 500);
            }
        }
    }


    xmlhttp.open('GET', url, false);
    xmlhttp.send();
}

Upvotes: 2

Related Questions