简体   繁体   English

Chrome扩展程序-数组中的XHR URL

[英]Chrome Extension - XHR URLs from an array

I have saved some subject URL keys to localStorage and now want to cycle through them a get content of each of them. 我已经将一些主题URL密钥保存到localStorage中,现在想循环浏览它们以获得每个主题的内容。

// Walk through saved subjects
allSubjects = JSON.parse(localStorage.getItem('subjects'));
var i = 0;
var ii = 0;

var xhrIn = [];
for (i = 0; i < allSubjects.length; i++) {
    xhrIn[i] = new XMLHttpRequest();
    xhrIn[i].open("GET", "https://myserver.com/" + allSubjects[i], true);
        xhrIn[i].onreadystatechange = function() {
            if (xhrIn[ii].readyState == 4) {
                console.log(xhrIn[ii].responseText);

                percents = Math.floor((((ii+1)/allSubjects.length)*100));
                $("div#status").text('Downloading... ' + percents + '%');

                // Final phase
                if ((ii+1) == allSubjects.length) {
                    $("div#status").text("All downloaded and saved in console.");
                }
                ii++;
            }
        };
        xhrIn[i].send();
    }
}

This is not working, it catches only the first URL, after that my Console log says, that all other URLs were contacted, but xhrIn[i].onreadystatechange closure has never been executed. 这是行不通的,它只捕获第一个URL,此后我的控制台日志说已联系了所有其他URL,但是从未执行过xhrIn [i] .onreadystatechange闭包。

It looks like a little bit magical for me... Can anyone explain me this behavior? 对我来说,这似乎有些不可思议。有人可以向我解释这种行为吗?

Haven't tested, but should be something like this: 还没有测试过,但是应该是这样的:

for (i = 0; i < allSubjects.length; i++) {
    xhrIn[i] = new XMLHttpRequest();
    xhrIn[i].open("GET", "https://myserver.com/" + allSubjects[i], true);

    xhrIn[i].onreadystatechange = (function(ii) {
        return function() {
            if (xhrIn[ii].readyState == 4) {
                console.log(xhrIn[ii].responseText);
            }
        };
    })(i);

    xhrIn[i].send();

}

Your current percent calculation will jump all over the place as callback functions could be called in random order. 您当前的百分比计算会随处可见,因为可以随机顺序调用回调函数。 You probably would need to rethink that part (create some global counter). 您可能需要重新考虑这一部分(创建一些全局计数器)。

Yes, I agree with epascarello, there are some fundamental problems with this code. 是的,我同意epascarello,此代码存在一些基本问题。 There is not guarantee that the callbacks assigned to run in the order you are intending. 不能保证分配的回调按照您想要的顺序运行。 If you want them to run in order, try something like this: 如果希望它们按顺序运行,请尝试以下操作:

var urls = ['test.php', 'test2.php', test3.php'];// and so on

function myRequest(){
    if(urls.length > 0){
        var nextUrl = urls.pop(); //TAKE THE NEXT URL (pop() removed from the end)
        var xhrIn = new XMLHttpRequest();
        xhrIn.open("GET", "https://myserver.com/" + nextUrl, true);
        xhrIn.onreadystatechange = function() {
            if (xhrIn.readyState == 4) {
                console.log(xhrIn.responseText);
                //THE FOLLOWING LINE WILL OBVIOUSLY NOT WORK ANY MORE
                //percents = Math.floor((((ii+1)/urls.length)*100));
                //$("div#status").text('Downloading... ' + percents + '%');
                myRequest(); //RECUR WHEN DONE WITH PREVIOUS REQUEST
            }
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM