XMLHttpRequest超时事件

《IE8 & 9开发实战:基于下一代IE的应用开发》第3章用AJAX和JSON来丰富Web应用程序的功能,本章将介绍Internet Explorer中的一些新功能和经过升级的功能,使开发人员能够增强动态Web应用程序的功能,并使其更为流畅。我们将会讨论兼容性和互操作性场景,重点讨论IE中可能会对现有应用程序产生影响的产品变化,并提供一些有用的详尽示例,用来演示如何将网站与IE中经过更新的AJAX功能集结合在一起。本节为大家介绍XMLHttpRequest超时事件。

AD: 2013大数据全球技术峰会课程PPT下载


    3.4.2  XMLHttpRequest超时事件

    Internet Explorer 8及更高版本在XMLHttpRequest对象中包含了timeout属性和ontimeout事件。使用这一对象的脚本可以使用这一属性对一个请求所花费的时间设置一个硬性限制,当达到这一限制时接收一个事件。

    属性

    Timeout属性--XMLHttpRequest对象从一个目标接收响应所应等待的时间限制(单位为毫秒)。当达到这一限制时,将产生ontimeout事件,并放弃该请求。

    事件

    Ontimeout事件--当XMLHttpRequest.send()的调用时间与当前时间之间的时间差达到某一限制时,则触发相关联的回调函数。

    代码清单3-16是一个网页,它使用服务器端脚本(在本例中为PHP)来模拟一个需要花费很长时间来加载的网页。当服务器接收到对这一页面的请求时,脚本暂停5秒(用sleep(5)实现),然后才返回文本"Success!"。

    代码清单3-16  强制服务器在响应之前等待5秒的网页

       
       
    1. <?php 
    2. // Simulate a page that takes 5 seconds to respond. Once finished,  
    3. // return the string "Success!"  
    4. sleep(5);  
    5. echo "Success!";  
    6. ?> 

    代码清单3-17给出了一段脚本,它使用两个XMLHttpRequest对象请求代码清单3-16中的页面,这两个对象是:xhrSmallTimeout和xhrLargeTimeout。xhrSmallTimeout对象请求上面的页面,并使用它的timeout属性将该请求的等待时间限制为2秒。xhrLargeTimeout对象尝试显示同一网页,但为其分配的等待时间最长可达10秒。每个对象都有两个回调方法:一个用于ontimeout (timeoutRaisedSmall()和timeoutRaisedLarge()),当一个请求超时时触发;另一个用于onreadystate (readyStateHandlerSmall()和readyStateHandlerLarge()),在请求/响应过程期间触发。在执行onreadystate回调期间产生的异常写到<div id="exceptions">。

    代码清单3-17  使用XMLHttpRequest超时事件的代码示例

       
       
    1. <html> 
    2. <head> 
    3. <title>XMLHttpRequest Timeout</title> 
    4. </head> 
    5. <body> 
    6. <h3>Request with 2s timeout: <span id="xhrSmallStatus"></span></h3> 
    7. <h3>Request with 10s timeout: <span id="xhrLargeStatus"></span></h3> 
    8. <h3>Exceptions:</h3> 
    9. <div id="exceptions"><p></div> 
    10. <script type="text/javascript"> 
    11. // Define the XMLHttpRequest variables, the target page and  
    12. // elements that will be written to  
    13. var xhrSmallTimeout; var xhrLargeTimeout;  
    14. var targetPage = "http://examples.proiedev.com/03/networking/ timeout/result.php";  
    15. var spanXhrSmallStatus = document.getElementById ("xhrSmallStatus");  
    16. var spanXhrLargeStatus = document.getElementById ("xhrLargeStatus");  
    17. var spanExceptions = document.getElementById("exceptions");  
    18. function displayException(e, objectName) {  
    19. spanExceptions.innerHTML += "<b>Object:</b> " + objectName + "<br>"  
    20. + "<b>Name:</b> " + e.name + "<br>"  
    21. + "<b>Message:</b> " + e.message + "<p>";  
    22. }  
    23. // Timeout callback for request with 2s timeout  
    24. function timeoutRaisedSmall(){  
    25. setText(spanXhrSmallStatus, "Timeout");  
    26. }  
    27. // Timeout callback for request with 10s timeout  
    28. function timeoutRaisedLarge() {  
    29. setText(spanXhrLargeStatus, "Timeout");  
    30. }  
    31. // readyState callback for request with 2s timeout. If an  
    32. // exception is raised, write it to the screen.  
    33. function readyStateHandlerSmall() {  
    34. if(xhrSmallTimeout.readyState == 4) {  
    35. try {  
    36. setText(spanXhrSmallStatus, xhrSmallTimeout.responseText);  
    37. } catch(e) { displayException(e, "xhrSmallTimeout"); }  
    38. }  
    39. }  
    40. // readyState callback for request with 10s timeout. If an  
    41. // exception is raised, write it to the screen.  
    42. function readyStateHandlerLarge() {  
    43. if(xhrLargeTimeout.readyState == 4) {  
    44. try {  
    45. setText(spanXhrLargeStatus, xhrLargeTimeout.responseText);  
    46. } catch(e) { displayException(e, "xhrLargeTimeout"); }  
    47. }  
    48. }  
    49. // Create a XMLHttpRequest object with a small (2 second)  
    50. // timeout period  
    51. xhrSmallTimeout = new XMLHttpRequest();  
    52. xhrSmallTimeout.open("GET", targetPage, true);  
    53. xhrSmallTimeout.timeout = 2000;  
    54. xhrSmallTimeout.ontimeout = timeoutRaisedSmall;  
    55. xhrSmallTimeout.onreadystatechange = readyStateHandlerSmall;  
    56. // Create a XMLHttpRequest object with a large (10 second)  
    57. // timeout period  
    58. xhrLargeTimeout = new XMLHttpRequest();  
    59. xhrLargeTimeout.open("GET", targetPage, true);  
    60. xhrLargeTimeout.timeout = 10000;  
    61. xhrLargeTimeout.ontimeout = timeoutRaisedLarge;  
    62. xhrLargeTimeout.onreadystatechange = readyStateHandlerLarge;  
    63. // Sent the XMLHttpRequests  
    64. xhrSmallTimeout.send(null);  
    65. xhrLargeTimeout.send(null);  
    66. // Set element text (cross-browser innerText/textContent)  
    67. function setText(element, text) { /*...*/ }  
    68. </script> 
    69. </body> 
    70. </html> 

    图3-12显示了所运行的示例代码。在xhrSmallTimeout对象发出请求之后2秒,调用了timeoutRaisedSmall()方法,并在页面上显示"Timeout"(因为服务器要等待至少5秒后才会响应)。在超时之后,触发onreadystate事件,产生一个异常--这个请求在技术上完成了(readyState==4),responseText返回一个null类型,而不是String。由xhrLargeTimeout发出的第二个请求在大约5秒后完成,触发readyStateHandlerLarge(),将相关联的responseText写到页面。由于第二个请求在10秒限制之前完成,所以从来不会调用timeoutRaisedLarge()方法。

     
    (点击查看大图)图3-12  演示XMLHttpRequest超时事件的网页
    这个例子指出了有关XMLHttpRequest超时的一个重点:如果事务在完成之前超时,浏览器会将其看作已经"完成",因此在触发onreadystatechange回调时,其readyState状态表示已经完成。但这些对象的responseXML和responseText属性是不可用的。使用timeout属性和ontimeout事件的脚本必须使用onreadystatechange事件处理程序,当这些属性为null时,这些处理程序可以正常地表示失败
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值