你能寫出來你使用超時的代碼,我寫了這樣的事情,但它不工作,所以我很好奇,你是怎麼做的吧:
var timeoutFired = function() {
console.log("derp");
};
var options = {
url: "http://somesite.com",
responseType: "document",
customRequestInitializer: function (req) {
req.timeout = 1;
req.ontimeout = timeoutFired;
//do something with the XmlHttpRequest object req
}
};
WinJS.xhr(options).
....
這裏有一些替代品,你可能會發現有幫助,不知道如何/爲什麼超時不工作,但我試圖寫出一個自定義超時功能:
(function (global) {
var options = {
url: "http://something.com",
responseType: "document",
};
var request = WinJS.xhr(options).then(
function (xmlHttpRequest) {
console.log("completed");
},
function (xmlHttpRequest) {
//error or cancel() will throw err
console.log("error"+ xmlHttpRequest.message);
},
function (xmlHttpRequest) {
console.log("progress")
});
function waitTime() {
return new WinJS.Promise(
function (complete, error, progress) {
var seconds = 0;
var interval = window.setInterval(
function() {
seconds++;
progress(seconds);
//prob should be called milliseconds
if (seconds > 5) {
window.clearInterval(interval);
complete();
}
}, 100);
});
};
waitTime().done(
function() {
console.log("complete");
request.cancel();
},
function() {
console.log("error")
},
function (seconds) {
console.log("progress:" + seconds)
});
});
另一個很酷的小技巧是使用promise.any(VS。加入),其觸發時關閉一個或其他飾面第一,所以考慮到這一點,你可以寫這樣的事情:
(function (global) {
var options = {
url: "http://url.com",
responseType: "document",
};
var request = {
runRequest: function() {
return WinJS.xhr(options).then(
function (xmlHttpRequest) {
console.log("completed");
},
function (xmlHttpRequest) {
//error or cancel() will throw err
console.log("error" + xmlHttpRequest.message);
},
function (xmlHttpRequest) {
console.log("progress")
});
}
};
WinJS.Promise.any([WinJS.Promise.timeout(500), request.runRequest()]).done(
function() {
console.log("any complete");
});
})();