2012-10-07 20 views
1

顯示JSON數據一個我所喜歡這個由一個

[ 
"00-00-02":"first one", 
"00-00-04":"the second", 
"00-00-09":"third", 
. 
. 
] 

如何顯示例如JSON數據:first one在指定的時間00-00-02,然後顯示下一個在其時間00-00-04
注意:我應該做的是使用JavaScript

+0

jsonvar [「00-00-02」](var持有JSON)應該返回「第一個」,所以我不知道這裏有什麼問題。 –

+0

@MihaRekar問題是計時這個文本。 – dotfreelancer

+0

我還是不明白。什麼時間? –

回答

1

@Geek Num 88提出了正確的方法。在這種情況下,通常使用unix時間戳。如果你不能改變的JSON,你可以做這樣的事情

var data = { 
"00-00-02":"first one", 
"00-00-04":"the second", 
"00-00-09":"third" 
} 
var timeout; 
var start = new Date(); 
function showIt(){ 
    var now = new Date(); 
    var diff = now - start; 
    var hh = Math.floor(diff/600000); 
    hh = (hh < 10)?"0" + hh : hh; 
    var mm = Math.floor((diff % 3600000)/60000); 
    mm = (mm < 10)?"0" + mm : mm; 
    var ss = Math.floor((diff % 60000)/1000); 
    ss = (ss < 10)?"0" + ss : ss; 

    if(data[hh + "-" + mm + "-" + ss]){ 
     console.log(data[hh + "-" + mm + "-" + ss]); 
    } 

    clearTimeout(timeout); 
    timeout = setTimeout(showIt, 1000); 
} 


showIt(); 
+0

大聲笑希臘語 - 這很有趣 - 只是怪胎很好 –

0

你剛纔JSON陣列?如果你已經JSON下面的工作:

var data = { 
    "00-00-02":"first one", 
    "00-00-04":"the second", 
    "00-00-09":"third" 
} 

Object.keys(data).forEach(function(key) { 
    var val = data[key]; 
    console.log("the " + val + " had a time of " + key + " seconds") 
}); 
+0

您沒有解決我的問題,我想在指定的時間顯示每個文本。 – dotfreelancer

+0

你必須添加更多的細節問題。它沒有提到任何有關視頻的內容。但是,請看[popcorn.js](http://popcornjs.org/)。它確實是你想要的。 – zemirco

1

如果你使用UNIX時間,你可以做的比較相當容易 - 例如

 
var data = { 
    "1349598808" : "first one", 
    "1349598845" : "the second", 
    "1349599400" : "third" 
}; 

var now; 
var seconds; 

setInterval(function(){ 
    now = new Date(); 
    seconds = Math.round(now.getTime()/1000); 
    if(data[seconds] != undefined) 
    { 
      console.log(data[seconds]); 
      // OR 
      // PrototypeJS 
      $("mydiv").update(data[seconds]); 
      // OR 
      // jquery 
      $("#mydiv").html(data[seconds]); 
    } 
},1000); 
​ 
+0

謝謝,但我怎麼能得到這樣的時間,我搜索了那個..但我什麼都不明白! – dotfreelancer

+0

哪些腳本正在生成您的頁面或ajax調用?大多數語言都可以將標準的「人類」時間轉換爲UNIX時間 –

+0

沒有語言,我從SQL Server數據庫中的存儲數據中獲得時間,但是如果您告訴我如何才能做到這一點,我可以更改時間列? – dotfreelancer

0

你可以嘗試使用setTimeout(fn, time)函數來調用「一線一個「,」秒「......經過計算的時間間隔。僞代碼會是這樣的

data = ["00-00-02":"first one", "00-00-04":"the second", "00-00-09":"third",...]; 
//using [key: value] notation 
for (i=0; i<data.length; i++) { 
    eventTime = timeInMilliSeconds(data[i].key); 
    setTimeout(printMyMessage(data[i].value, eventTime); 
} 

,或者你也許可以致電setTimeout()data[i]在函數調用printMyMessage()data[i-1]。在這種情況下setTimeout()函數的第二個參數將是data[i]data[i-1]之間的時差。