Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Javascript - SetInterval + xhr multipli not working
Forum - Javascript - SetInterval + xhr multipli not working

Avatar
giocala88 (Normal User)
Expert


Messaggi: 248
Iscritto: 23/04/2008

Segnala al moderatore
Postato alle 11:22
Martedì, 18/11/2014
Salve ragazzi ho il seguente problema ...

Funzione AJAX Send HttpRequest in JS:

Codice sorgente - presumibilmente Delphi

  1. function GET( action, successHandler, errHandler ) {
  2.         xhr.open( 'GET', action+'?t='+ Math.random(), true );
  3.         var responseTypeAware = 'responseType' in xhr;
  4.         if (responseTypeAware) {
  5.                 xhr.responseType = 'json';
  6.         }
  7.         xhr.onreadystatechange = function() {
  8.         var status = xhr.status;
  9.         var data;
  10.         if ( xhr.readyState == 4 ) {
  11.                 if ( status == 200 ) {
  12.                         successHandler && successHandler(
  13.                         responseTypeAware
  14.                         ? xhr.response
  15.                         : JSON.parse(xhr.responseText)
  16.                         );
  17.                 } else {
  18.                 errHandler && errHandler(status);
  19.                 }
  20.         }
  21. };
  22. xhr.send();                            
  23.                        
  24.                 }



Dovrei implemnetare una cosa del tipo:

Codice sorgente - presumibilmente Plain Text

  1. setInterval( function() { GET( action, successHandler, errHandler ); }, 1000);
  2. 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.

PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 15:08
Martedì, 18/11/2014
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: https://piero.dev
PM Quote
Avatar
giocala88 (Normal User)
Expert


Messaggi: 248
Iscritto: 23/04/2008

Segnala al moderatore
Postato alle 15:34
Martedì, 18/11/2014
Risolto. Grazie mille :k:

PM Quote