One approach that we have been using to trigger a URL through ajax involves receiving the response immediately after hitting the URL. However, there is now a requirement to implement a timeout for the response. This means that if the response does not arrive within 2 seconds of invoking the URL, another method should be called. On the other hand, if the response comes back within two seconds, a different method should be executed instead. Despite our attempts at incorporating the timeout feature, we have encountered difficulties in getting it to function properly with the current code.
function ajaxCall(url1) {
var ajaxresp = null;
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", url1, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
ajaxresp = xmlhttp.responseText;
//function call
getAdResponse(ajaxresp);
}
}
}
}
We are seeking advice on how best to proceed given this scenario.