cocos creator 网络http请求超时处理
async http_post(url,parme) {
return new Promise((resolve,reject)=>{
var xhr = cc.loader.getXMLHttpRequest();
var time = false;//是否超时
var timer = setTimeout(function(){
time = true;
xhr.abort();//请求中止
resolve('error');
},5000);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
var respone = xhr.responseText;
if(time) return;//请求已经超时,忽略中止请求
clearTimeout(timer);//取消等待的超时
resolve(respone);
xhr.open("POST", url, true);
// note: In Internet Explorer, the timeout property may be set only after calling the open()
// method and before calling the send() method.
xhr.timeout = 5000;// 5 seconds for timeout
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//let parme = 123;
xhr.send(parme);
xhr.ontimeout = function (e) {
// XMLHttpRequest 超时。在此做某事。
console.log('ontimeout')
resolve('error');
在微信小游戏中,不识别。不过在web端可以使用