首页 > 代码库 > 解决XMLHttpRequest的timeout在firefox<12版本无效

解决XMLHttpRequest的timeout在firefox<12版本无效

XMLHttpRequest的timeout在firefox<12 版本无效的,可以重写XMLHttpRequest的timeout对象

// snipto = function() {    attempt++;    if( attempt < 5)        send();    else if( !silent) {        console.log("Request Timeout\nFailed to access "+url);    }};// snipvar send = function() {    if( loader && attempt != 0) {        loader.children[0].firstChild.nodeValue = "http://www.mamicode.com/Error... retrying...";        loader.children[1].firstChild.nodeValue = "http://www.mamicode.com/Attempt"+(attempt+1)+" of 5";    }    a = new XMLHttpRequest();    a.open("POST","/ajax/"+url,true);    a.onreadystatechange = rsc;    setTimeout(function () {     /* vs. a.timeout */        if (a.readyState < 4) {            a.abort();        }    }, 5000);    a.onabort = to;              /* vs. a.ontimeout */    a.setRequestHeader("Content-Type","application/json");    a.send(data);    console.log(‘HTTP Requesting: %s‘, url);};