Ok, quindi iniziamo con il codice. Ho aggiunto alcuni commenti ad esso, così ora si può capire l'origine del problema:
var A = new XMLHttpRequest(); //You create an XMLHttpRequest object
var B = new XMLHttpRequest(); //And an another
A.open("GET", directory, true);
/* Now you open a GET request to DIRECTORY, with async TRUE. The third parameter can
make a request sync or async, but sync is not recommended as described below. */
A.onreadystatechange = function() {
if (A.readyState === 4) {
if (A.status === 200 || A.status == 0) {
/* So you registered an event listener. It runs when the readyState changes.
You can use it to detect if the request is finished or not. If the readyState is
4, then the request is finished, if the status code is 200, then the response is
OK. Here you can do everythin you want after the request. */
}
}
}
A.send(null); //Now you send the request. When it finishes, the event handler will
// do the processing, but the execution won't stop here, it immediately goes to the
// next function
while(true){ // Infinite loop
B.open("GET", another_directory, false); //Open request B to ANOTHER_DIRECTORY,
// but now, request B will be synchronous
B.overrideMimeType("application/document"); // Configure mime type
B.send(null); // Send the request
if (B.status == "404")
continue;
// If it's not found, then go to the next iteration
// and do something else
}
Spero che ora si può vedere l'origine del problema. Quando si esegue questo script, quindi si avvia una richiesta asincrona e quindi si avvia immediatamente il successivo. Ora puoi scegliere tra 2 modi.
Run successiva richiesta da richiamata (consigliato)
E 'il modo migliore. Quindi inizia la tua prima richiesta (asincrona) e nel listener dell'evento (dove esegui l'elaborazione) puoi avviare la richiesta successiva. Ho fatto un esempio commentato qui: http://jsfiddle.net/5pt6j1mo/1/
(lo si può fare senza di matrici - era solo un esempio)
Se si utilizza in questo modo, allora l'interfaccia grafica non si blocca fino a quando si è in attesa per risposta. Tutto sarà responsabile in modo da poter interagire con la pagina, è possibile creare pulsante di annullamento, ecc
sincrono AJAX (non raccomandato)
io non lo consiglio perché "XMLHttpRequest sincrono sul thread principale è deprecato "in Chrome, ma se vuoi davvero, puoi provare a utilizzare questa soluzione. Così la funzione aperta di un XMLHttpRequest ha 3 argomenti:
- METODO: quali HTTP methid da usare
- URL: l'URL per richiedere
- ASYNC: richiesta asincrona? Se false, sarà sincrono, il che significa che dopo aver chiamato .send(), sospenderà l'esecuzione fino a quando la risposta non ritorna.
Quindi, se si imposta il terzo parametro su FALSE, è possibile farlo facilmente ... ma non si dovrebbe!
Javascript è asincrono. Hai bisogno di conoscere i callback ... fai una funzione, al termine della richiamata alla funzione B ... – zipzit
a seconda del browser che stai usando e se ciò su cui stai lavorando non sarà pronto per l'uso molto presto, e specialmente se questo è per te suggerirei di imparare a usare 'fetch': http://updates.html5rocks.com/2015/03/introduction-to-fetch –