Why, when executing this cycle, I get the result of only the last xhr query? How to fix it?
for(var i = 0;i<urls.length;i++) { xhr1 = new XMLHttpRequest(); xhr1.open("GET", urls[i], true); xhr1.send(null); xhr1.onreadystatechange = function() { if (xhr1.readyState == 4) { console.log(xhr1.status); if (xhr1.responseText) { var dat = xhr1.responseText; var str = dat.substring(dat.indexOf('filehd')+9,dat.indexOf('.mp4',dat.indexOf('filehd'))+4); $('#wrapper').append(str+'\n'); } } } }
асинхронными, are executed in parallel, javascript does not wait for the end of the request. You can makeсинхронныеrequests. To do this, replace the second parameter in the functionxhr1.openwithtruetofalse:xhr1.open("GET", urls[i], false);Но синхронные запросы считаются плохой практикой и в современных браузерах могут не работать в главном потоке.There are also options with asynchronous requests, but too complex for comment. - Sergeyonreadystatechangeis the same callback. He is not waiting for anything by definition. It is called by the browser when it sees fit. - Sergey