Ho un database e memorizzo un testo, la sua durata e il momento in cui dovrebbe apparire sulla pagina web.Come posso visualizzare gli elementi di stringa ricevuti da json in base alla data e all'ora che dovrebbero essere visualizzati?
Il risultato del php è la seguente:
{"text_content":"dgsgdsgds","text_duration":"15","start_time":"2015-09-28 23:11:15"},{"text_content":"dgsgdsgds","text_duration":"15","start_time":"2015-09-28 23:11:30"},{"text_content":"gdsgdsgds","text_duration":"15","start_time":"2015-10-01 14:00:00"}
Ho uno script jQuery che recupera i dati dal database e lo stampa sullo schermo:
var results =[];
var cursor = 0;
function myFunction() {
$.getJSON('list2.php', function(json) {
results = json;
cursor = 0;
// Now start printing
printNext();
});
}
function printNext(){
if(cursor == results.length){
// Reset the cursor back to the beginning.
cursor = 0;
}
// Print the key1 in the div.
//$('#mydiv').html(results[cursor].key1);
$('#mydiv').hide('fast', function(){ $('#mydiv').html(results[cursor].text_content); $('#mydiv').show('fast'); });
// Set a delay for the current item to stay
// Delay is key2 * 1000 seconds
setTimeout(function(){
printNext();
}, results[cursor].text_duration * 1000);
// Advance the cursor.
cursor++;
}
e ora volevo per aggiungere una funzionalità che il testo viene visualizzato sullo schermo solo in una data recuperata dal database, come start_time
, ma non sono sicuro che sia possibile eseguire solo su jquery, senza ulteriore accesso al codice del server. I ho provato ad aggiungere alcune affermazioni come:
if(results[cursor].start_time == new Date()){
poco prima della stampa sullo schermo, ma non ha fatto il trucco. Potresti aiutarmi con quello? Grazie!
meglio prendere solo i dati richiesti in base al tempo da DB. @ randomuser1 –
Ci ho pensato, ma voglio fare una query ricorrente che recuperi i dati per ogni ora e poi visualizza i dati in base ai minuti di quell'ora .. – randomuser1