简体   繁体   English

前循环XMLHttpRequest记录值混乱

[英]For-Loop XMLHttpRequest logs values out of order

I need this code to find all URL's from several webpages and list them in a certain order in console. 我需要此代码来从几个网页中找到所有URL,并在控制台中以特定顺序列出它们。 I need the list to begin with URL's from fakeURL.com/0 and end with fakeURL.com/20, and stay in order all the way. 我需要列表以假URL.com/0中的URL开头,并以fakeURL.com/20结尾,并始终保持顺序。 The problem is that sometimes it will list URL's from (for example) fakeURL.com/5 before URL's from fakeURL.com/2. 问题在于,有时它会列出(例如)fakeURL.com/5中的URL,然后再列出来自fakeURL.com/2中的URL。

It also needs to be in order within each webpage - URL's that are more near the top of a webpage should come first. 还需要在每个网页中按顺序排列-应该更接近网页顶部的URL。

What's causing the list to be out of order, and how can I fix it? 是什么导致列表混乱,如何解决?

var i;
function ajaxCall (x)
{
        var xhrs = new XMLHttpRequest();                                    
        xhrs.open("get", 'http://fakeURL.com/' + x, true);
        xhrs.onload = function()
        {       
            var doc = xhrs.response;
            $(doc).find('a').each(function()
            {    

                var url = $(this).attr('href');
                console.log(url);

            });

        }
        xhrs.responseType = 'document';
        xhrs.send();
}

for(i = 0; i <= 20; i++) 
{   
    ajaxCall(i);
}

The XMLHttpRequest by default is asyncronous. 默认情况下, XMLHttpRequest是异步的。 So, if you call ajaxCall() with 1,2,3,...20 (for your particular case) this doesn't guarantee you that the URL is printed (console.log) in the same sequence. 因此,如果您用1,2,3,... 20(针对您的特定情况)调用ajaxCall() ,则不能保证您以相同的顺序打印URL(console.log)。

For more information, read this documentation from mozilla 有关更多信息,请从mozilla阅读此文档。

The reason why you get the values out of order despite traversing incrementally in a for loop is because that's how an XMLHttpRequest works by default (ie asynchronously) 尽管在for循环中递增遍历,但仍使值混乱的原因是因为XMLHttpRequest是默认情况下(即异步)的工作方式

From the official documentation , in an Asynchronous HTTP Request, the elements don't freeze while the request happens in the background and once the resources are fetched, you can tap on them by using a callback function. 根据官方文档 ,在异步 HTTP请求中,当请求在后台发生时,元素不会冻结,一旦获取资源,就可以使用callback函数来点击它们。

Synchronous requests block the execution of code which creates "freezing" on the screen and an unresponsive user experience. 同步请求阻止了代码的执行,该代码在屏幕上产生“冻结”,并导致无响应的用户体验。

So Sychronous requests might seem the way to go in your case, but they have performance implications and of course may account for a bad user experience. 因此,在您的情况下,同步请求似乎是行之有效的方法,但它们会影响性能,并且当然可能会导致不良的用户体验。

A simple work around that I can suggest for your case which I understand is just listing the URLs, is just let the URLs get fetched in whatever way they want to. 我可以为您的情况建议一个简单的解决方法,据我了解,它只是列出URL,只是让URL以它们想要的任何方式获取。 Store them in an array and add another attribute to them called page_id or something that can help you identify the order of your links. 将它们存储在数组中,并向它们添加另一个属性,称为page_id或可以帮助您确定链接顺序的属性。 So the outermost links could look something like: 因此,最外部的链接可能类似​​于:

var a = {link: "http://fakeURL.com/1", page_id: 1};

//Store such objects in a list.

For internal links on one page as well, let them get parsed in whatever they want to, and just associate their indices with them using .index() in Jquery. 对于一页上的内部链接,也让它们以任何想要的方式进行解析,然后只需在Jquery中使用.index()它们的indices与它们相关联.index() From the documentation : 文档中

 //html 
<ul>
 <li id="foo">foo</li>
 <li id="bar">bar</li>
 <li id="baz">baz</li>
</ul>

//Javascript
var listItem = document.getElementById( "bar" );
alert( "Index: " + $( "li" ).index( listItem ) );

//Outputs - Index: 1

Now when you need to display the links, just sort the relevant lists with the page_id attribute or the depth attribute and display them accordingly. 现在,当您需要显示链接时,只需对具有page_id属性或depth属性的相关列表进行排序,并进行相应显示。 Hope it gets you started in the right direction. 希望它能帮助您朝正确的方向开始。

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

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