2009-10-06 11 views
29

मैं निम्न स्क्रिप्ट में टाइमआउट कैसे जोड़ सकता हूं? मैं इसे "टाइम आउट" के रूप में टेक्स्ट प्रदर्शित करना चाहता हूं।टाइमआउट XMLHttpRequest

var bustcachevar = 1 //bust potential caching of external pages after initial request? (1=yes, 0=no) 
var loadedobjects = "" 
var rootdomain = "http://" + window.location.hostname 
var bustcacheparameter = "" 

function ajaxpage(url, containerid) { 
    var page_request = false 
    if (window.XMLHttpRequest) // if Mozilla, Safari etc 
     page_request = new XMLHttpRequest() 
    else if (window.ActiveXObject) { // if IE 
     try { 
      page_request = new ActiveXObject("Msxml2.XMLHTTP") 
     } catch (e) { 
      try { 
       page_request = new ActiveXObject("Microsoft.XMLHTTP") 
      } catch (e) {} 
     } 
    } else 
     return false 
    document.getElementById(containerid).innerHTML = '<img src="load.gif" border="0"><br><br><strong>Generating Link...</strong>' 
    page_request.onreadystatechange = function() { 
     loadpage(page_request, containerid) 
    } 
    if (bustcachevar) //if bust caching of external page 
     bustcacheparameter = (url.indexOf("?") != -1) ? "&" + new Date().getTime() : "?" + new Date().getTime() 
    page_request.open('GET', url + bustcacheparameter, true) 
    page_request.send(null) 
} 


function loadpage(page_request, containerid) { 
    if (page_request.readyState == 4 && (page_request.status == 200 || window.location.href.indexOf("http") == -1)) 
     document.getElementById(containerid).innerHTML = page_request.responseText 
    else if (page_request.readyState == 4 && (page_request.status == 404 || window.location.href.indexOf("http") == -1)) 
     document.getElementById(containerid).innerHTML = '<strong>Unable to load link</strong><br>Please try again in a few moments' 
} 
+0

एक इस सवाल का बहुत अच्छा जवाब यहाँ है: http://stackoverflow.com/questions/1018705/how-to-detect-timeout-on-an-ajax-xmlhttprequest-call-in- ब्राउज़र –

उत्तर

73

उदाहरण के लिए XMLHttpRequest ऑब्जेक्ट के टाइमआउट गुणों का उपयोग कर।

var xhr = new XMLHttpRequest(); 

xhr.onreadystatechange = function() { 
    if (xhr.readyState == 4) { 
     alert("ready state = 4"); 
    } 
}; 

xhr.open("POST", "http://www.service.org/myService.svc/Method", true); 
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8"); 
xhr.timeout = 4000; // Set timeout to 4 seconds (4000 milliseconds) 
xhr.ontimeout = function() { alert("Timed out!!!"); } 
xhr.send(json); 

उपर्युक्त कोड मेरे लिए काम करता है!

चीयर्स

+1

कोड संपादित किया गया: टाइमआउट हैंडलर को 'टाइमआउट' पर नहीं, 'ऑनटाउट' पर सेट किया जाना चाहिए। (एक ही चीज़ की तलाश करते समय इस पोस्ट को मिला, [एमएसडीएन] (http://msdn.microsoft.com/en-us/library/ie/cc304105%28v=vs.85%29.aspx) पुष्टि करता है)। –

+0

@erikvold 'timeout'/'ontimeout' फ़ायरफ़ॉक्स में भी समर्थित है, फ़ायरफ़ॉक्स 12.0 से शुरू: https://developer.mozilla.org/en/xmlhttprequest#Browser_compatibility –

+11

एक्सएचआर के साथ एक इंटरमीटेंट त्रुटि देखने वाले किसी और के लिए बस एक नोट और IE10 - xhr.timeen मान xhr.open कथन के बाद सेट किया जाना चाहिए। यदि आईई 10 ने अंततः एक त्रुटि फेंकने से पहले टाइमआउट मान सेट किया है। – paj

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^