2012-09-11 4 views
5

Perché i seguenti funzionano correttamente in Firefox ma NON in Chrome? Se cambio il valore restituito nel ciclo .each su "true", funziona in Chrome e non in Firefox. Cosa dà?

(Quando non funziona, restituisce solo in pochi secondi contro il giorno corretto, ora, ecc.)

function time_remaining(expire_time) 
    { 
     var now = new Date().getTime()/1000, 
     time_left = expire_time - now, 
     time_left_str = '0 seconds'; 

     if (time_left < 1) { 
      return time_left_str; 
     } 

     var a = {}; 
      a[ 12 * 30 * 24 * 60 * 60 ] = 'year', 
      a[  30 * 24 * 60 * 60 ] = 'month', 
      a[   24 * 60 * 60 ] = 'day', 
      a[    60 * 60 ] = 'hour', 
      a[      60 ] = 'minute', 
      a[      1 ] = 'second'; 


     $.each(a, function (secs, str) { 
      var d = time_left/secs; 
      if (d >= 1) { 
       var r = Math.round(d); 
       time_left_str = r + ' ' + str + ((r > 1) ? 's' : ''); 
       return false; 
      } 
     }); 

     return time_left_str; 
    } 
+1

Secondo la documentazione 'Siamo in grado di interrompere il ciclo $ .each() in una particolare iterazione rendendo la funzione di callback restituita falsa. Il ritorno non-falso è lo stesso di un'istruzione continue in un ciclo for; salterà immediatamente alla successiva iterazione. – MrOBrian

+0

Prova a usare return (fasle) come callback alla tua funzione .. Questo dovrebbe risolvere il tuo problema in quanto agirà come una pausa; –

+0

Ho provato sia "return false" che "return (false)". Questo funziona correttamente su Firefox. Tuttavia, NON funziona correttamente su Chrome. Chrome funziona correttamente solo quando uso "return true". Non sono sicuro? –

risposta

3

Non è $.each problema, var a = {}; è un oggetto e $.each(a, function (secs, str) {}) sempre fallito in qualche modo, perché jQuery ogni funzione accetta solo un array.

necessario convertire object in array quindi utilizzare la funzione $.each.

Aggiunto nuovo codice nel corpo della funzione per fare object a array

// converting object to array 
var temp = []; 
var finalArr = []; 
for (val in a) { 
    temp.push(val); 
} 

temp = temp.sort(function (a, b) { 
    return a - b 
}); 
for (var i = 0; i < temp.length; i++) { 
    finalArr.push(a[temp[i]] + "|" + temp[i]); 
} 

e modificato anche il modo di accesso ai dati e il valore di indice del nuovo array creato

function time_remaining(expire_time) { 
    var now = new Date().getTime()/1000, 
     time_left = expire_time - now, 
     time_left_str = '0 seconds'; 

    if (time_left < 1) { 
     return time_left_str; 
    } 

    var a = {}; 
    a[12 * 30 * 24 * 60 * 60] = 'year', 
    a[30 * 24 * 60 * 60] = 'month', 
    a[24 * 60 * 60] = 'day', 
    a[60 * 60] = 'hour', 
    a[60] = 'minute', 
    a[1] = 'second'; 


    // converting object to array 
    var temp = []; 
    var finalArr = []; 
    for (val in a) { 
     temp.push(val); 
    } 

    temp = temp.sort(function (a, b) { 
     return a - b 
    }); 
    for (var i = 0; i < temp.length; i++) { 
     finalArr.push(a[temp[i]] + "|" + temp[i]); 
    } 

    $.each(finalArr, function (secs, str) { 
     var time = time_left/parseInt(str.split("|")[1], 10); 

     if (time >= 1) { 

      var randomNum = Math.round(time); 
      time_left_str = randomNum + ' ' + str.split("|")[0] + ((randomNum > 1) ? 's' : ''); 
      return true; 
     } 
    }); 

    return time_left_str; 

} 
+1

Questo funziona. Strano come Chrome e FF gestiscono diversamente. Grazie KK. –

Problemi correlati