XMLHttpRequest

CDNSpeed项目 记录接口响应时间和文件大小。

 request: function (method, url, headers) {
                var startTime = new Date().getTime();
                return $q(function (resolve, reject) {
                    var xhr = new XMLHttpRequest();
                    xhr.open(method, url, true);
                    if (typeof headers === 'object') {
                        for (var key in headers) {
                            xhr.setRequestHeader(key, headers[key]);
                        }
                    }
                    xhr.onreadystatechange = function (e) {
                        if (xhr.readyState == 4) {
                            if (xhr.status == 200) {
                                resolve({ size: xhr.response.length, time: new Date().getTime() - startTime });
                            } else {
                                reject();
                            }
                        }
                    };
                    xhr.send();
                });
            },

 

posted @ 2018-05-04 09:46  白洋花海  阅读(167)  评论(1编辑  收藏  举报