Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set time out in ajax call while using core javascript

I have a JavaScript function to call ajax. Now I need to add time out in this function like while calling service took more than defile time ajax call should time out and display a default message. I don't want to use Jquery in it.

here is my code:

AJAX = function (url, callback, params) {
        var dt = new Date();
        url = (url.indexOf('?') == -1) ? url + '?_' + dt.getTime() : url + '&_' + dt.getTime();
        if (url.indexOf('callback=') == -1) {
            ajaxCallBack(url, function () {
                if (this.readyState == 4 && this.status == 200) {
                    if (callback) {
                        if (params) {
                            callback(this.responseText, params);
                        } else {
                            callback(this.responseText);
                        }
                    }
                }
            });
        } else {
            var NewScript = d.createElement("script");
            NewScript.type = "text/javascript";
            NewScript.src = url + '&_' + Math.random();
            d.getElementsByTagName('head')[0].appendChild(NewScript);
        }
    },
    ajaxCallBack = function (url, callback) {
        var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = callback;
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
    }
like image 323
Yogendra Avatar asked Dec 18 '15 07:12

Yogendra


People also ask

Does AJAX call have timeout?

The jQuery ajax timeout option is used to specifies that the number of milliseconds a request should wait for automatically being terminated. The jQuery ajax timeout option is a built-in option that is passed to the ajax() function in the jQuery.

How do you call AJAX every 5 seconds?

Creating a function that calls the AJAX request and using this function in setInterval() and set Interval for 5 sec. Now the function executes every 5 seconds and fetches new data from the server. It repeatedly executes the function even when the previous AJAX request is not successfully executed and return.

How to set time in AJAX?

This can be achieved by using jQuery setTimeout() function. This function executes the given Ajax code after some amount of given time. Syntax : $.

What is default AJAX timeout?

The executionTimeout attribute exists under httpRequest in the Machine.config file. Set a local timeout (in milliseconds) for the request. The default Ajax request is set to 90 seconds.


1 Answers

Here's an example of how you can handle a timeout:

var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "http://www.example.com", true);

xmlHttp.onreadystatechange=function(){
   if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
      clearTimeout(xmlHttpTimeout); 
      alert(xmlHttp.responseText);
   }
}
// Now that we're ready to handle the response, we can make the request
xmlHttp.send("");
// Timeout to abort in 5 seconds
var xmlHttpTimeout=setTimeout(ajaxTimeout,5000);
function ajaxTimeout(){
   xmlHttp.abort();
   alert("Request timed out");
}

In IE8, You can add a timeout event handler to the XMLHttpRequest object.

var xmlHttp = new XMLHttpRequest();
xmlHttp.ontimeout = function(){
  alert("request timed out");
}

Use a javascript framework to do this though, i don't know why you're not using one, do you like uneccesary work? :)

like image 110
Dan Hall Avatar answered Sep 28 '22 05:09

Dan Hall