Oppure

Loading
18/11/14 10:22
giocala88
Salve ragazzi ho il seguente problema ...

Funzione AJAX Send HttpRequest in JS:

function GET( action, successHandler, errHandler ) {
	xhr.open( 'GET', action+'?t='+ Math.random(), true );
	var responseTypeAware = 'responseType' in xhr;
	if (responseTypeAware) {
		xhr.responseType = 'json';
	}
	xhr.onreadystatechange = function() {
	var status = xhr.status;
	var data;
	if ( xhr.readyState == 4 ) { 
		if ( status == 200 ) {
			successHandler && successHandler(
			responseTypeAware
			? xhr.response
			: JSON.parse(xhr.responseText)
			);
		} else {
		errHandler && errHandler(status);
		}
	}
};
xhr.send();				
			
		}


Dovrei implemnetare una cosa del tipo:

setInterval( function() { GET( action, successHandler, errHandler ); }, 1000);
setInterval( function() { GET( action2, successHandler, errHandler ); }, 2000);


Il problema è che la prima setInterval non la considera proprio a differenza della seconda (o ultima) che la elabora correttamente. Il problema è il caching e per questo ho inserito t='+ Math.random() ma nulla ...soluzioni?

Grazie.
aaa
18/11/14 14:08
pierotofy
xhr e' un oggetto globale e probabilmente crea delle race condition. Crea un istanza di xhr per ogni richiesta e dovrebbe funzionare.
Il mio blog: piero.dev
18/11/14 14:34
giocala88
Risolto. Grazie mille :k:
aaa