Timeout XMLHttpRequest

后端 未结 1 835
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 06:10

How can I add a timeout to the following script? I want it to display text as \"Timed Out\".

var bustcachevar         


        
相关标签:
1条回答
  • 2020-11-27 06:18

    using the timeout properties of XMLHttpRequest object for example.

    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);
    

    the above code works for me!

    Cheers

    0 讨论(0)
提交回复
热议问题