XHR .timeout不起作用

时间:2017-06-09 17:34:45

标签: ionic-framework xmlhttprequest

我正在研究我的离子(1)应用程序,我在手机上进行本地开发。

我正在尝试使用以下方法模拟XHR请求超时:

request.timeout = 1;

但它没有转到.ontimeout() ...

有什么想法吗?

这里有完整的功能:

function getDBfileXHR(url, serverAttempts) {
                    return new Promise(function (resolve, reject) {
                        var request = new XMLHttpRequest();
                        request.open("GET", url, true); 
                        request.timeout = 1;

                        if (url === "proDB.jsonp") {
                            console.log("-Asking local proDB.json...");
                        } else {
                            console.log("-Sending XMLHttpRequest...");
                        }
                        request.ontimeout = function () {
                            console.error("Timeout : the XHR request timed out !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! serverAttempts = " + serverAttempts);
                            reject({
                                status: this.status,
                                statusText: request.statusText
                            });
                        };
                        request.onreadystatechange = function(event) {
                            switch (this.readyState) {
                                case 0:
                                    console.warn('-XMLHttpRequest readyState is UNSENT');
                                    break;
                                case 1:
                                    console.warn('-XMLHttpRequest readyState is OPENED');
                                    break;
                                case 2:
                                    console.warn('-XMLHttpRequest readyState is HEADERS_RECEIVED');
                                    break;
                                case 3:
                                    console.warn('-XMLHttpRequest readyState is LOADING');
                                    break;
                                case 4:
                                    console.warn('-XMLHttpRequest readyState is DONE');
                                    break;
                                default:
                            }
                        };
                        request.onload = function () {
                            if ( (request.readyState === 4) && ( (request.status >= 200 && request.status <= 299) || request.status === 304 || request.status === 0) ) {
                                console.log('-we get response '+request.status+' from XHR in getDBfileXHR');
                                if (request.status === 304) sendToServer.sendErrorEmail("BL: request.status === 304 juste pour info", 21);
                                var jsonText = request.responseText.replace("callback(", "").replace(");", "");


                                if (jsonText === '') {
                                    console.error('-error : request.status = ' + request.status + ', but jsonText is empty for url=' + url);


                                        sendToServer.sendErrorEmail("BL: jsonText is empty, trying to reach server another time", 11);






                                        sendToServer.sendErrorEmail("BL: jsonText is empty and attempted to reach server more than twice", 14);
                                        var alertPopup = $ionicPopup.alert({
                                            title: 'Error '+"11, jsonText is empty",
                                            template: "The surfboard database could not be updated, you won't see the new models in the list, sorry for the inconvenience.",
                                            buttons: [{
                                                text:'OK',
                                                type: 'button-light'
                                            }]
                                        });





                                        reject({
                                          status: this.status,
                                          statusText: request.statusText
                                        });




                                } else {
                                    var parsedJson;
                                    try {
                                        parsedJson = JSON.parse(jsonText);
                                    } catch (e) {
                                        console.warn("Problem when trying to JSON.parse(jsonText) : ");
                                        console.warn(e);
                                        console.warn("parsedJson :");
                                        console.warn(parsedJson);

                                    }
                                    if (parsedJson) {
                                        var prodata = jsonToVarProdata(parsedJson);


                                        console.log('-writing new prodata to localStorage');                            
                                        console.log('last line of prodata:' + JSON.stringify(prodata[prodata.length-1]));
                                        storageService.persist('prodata', prodata);
                                        storageService.store('gotANewDB', 1);
                                    } else {
                                        console.error ('WARNINNNNG ! parsedJson is not good...can t write prodata to localStorage');
                                    }

                                    resolve(request.response);
                                    dbReadyDeferred.resolve();
                                }
                            } else {
                                console.log('-bad request, request.readyState= ' + request.readyState  + ' and resquest.status = '+request.status+' from XHR in getDBfileXHR');
                                sendToServer.sendErrorEmail("BL: request.status is bad, no boards are displayed in the list !!!!", 41);
                            }
                        };
                        request.onerror = function () {
                            reject({
                                status: this.status,
                                statusText: request.statusText
                            });
                        };
                        console.log("sending request.send()");
                        request.send();

                    });
                }

1 个答案:

答案 0 :(得分:0)

.timeout

似乎至少有10毫秒