Ho una funzione JavaScript per chiamare ajax. Ora ho bisogno di aggiungere il timeout in questa funzione, come quando il servizio di chiamata ha richiesto più tempo di defilare che la chiamata ajax dovrebbe scadere e visualizzare un messaggio predefinito. Non voglio usare Jquery in esso.impostato il timeout nella chiamata ajax durante l'utilizzo di core javascript
Ecco il mio codice:
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();
}
Grazie per il tuo aiuto ... ma non funziona nel mio codice come mostrato sopra ... puoi modificare il mio codice .... – Yogendra
Dopo aver modificato un po ', ora funziona ... grazie ancora. – Yogendra