Javascript 逐个显示JSON数据

Javascript 逐个显示JSON数据,javascript,json,Javascript,Json,我有这样的JSON数据 [ "00-00-02":"first one", "00-00-04":"the second", "00-00-09":"third", . . ] 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 Dat

我有这样的JSON数据

[
"00-00-02":"first one",
"00-00-04":"the second",
"00-00-09":"third",
.
.
]
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();
如何显示例如:
在指定时间内第一个
00-00-02
,然后在其时间内显示下一个
00-00-04


注意:我应该使用javascript来做这件事

你有一个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")
});

如果您使用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); ​ 风险值数据={ “1349598808”:“第一个”, “1349598845”:“第二个”, “1349599400”:“第三” }; var现在; var秒; setInterval(函数(){ 现在=新日期(); 秒=Math.round(now.getTime()/1000); 如果(数据[秒]!=未定义) { 日志(数据[秒]); //或 //原型JS 更新(数据[秒]); //或 //jquery $(“#mydiv”).html(数据[秒]; } },1000); ​
您可以尝试使用
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);
}
data=[“00-00-02”:“第一个”、“00-00-04”:“第二个”、“00-00-09”:“第三个”、…];
//使用[key:value]表示法

for(i=0;i@Geek Num 88建议正确的方法。在这种情况下,通常使用unix时间戳。如果您无法更改json,您可以这样做

[
"00-00-02":"first one",
"00-00-04":"the second",
"00-00-09":"third",
.
.
]
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();
var数据={
“00-00-02”:“第一个”,
“00-00-04”:“第二个”,
“00-00-09”:“第三”
}
var超时;
var start=新日期();
函数showIt(){
var now=新日期();
var diff=现在-开始;
var hh=数学地板(差值/600000);
hh=(hh<10)?“0”+hh:hh;
var mm=数学地板(差值%3600000)/60000);
毫米=(毫米<10)?“0”+毫米:毫米;
var ss=数学下限((差值%60000)/1000);
ss=(ss<10)?“0”+ss:ss;
如果(数据[hh+“-”+mm+“-”+ss]){
日志(数据[hh+“-”+mm+“-”+ss]);
}
clearTimeout(超时);
超时=设置超时(showIt,1000);
}
showIt();

您应该知道视频字幕已经是一种东西了,您不需要重新实现它们

您可以使用HTML5中的
track
元素,或者如果您需要更高的兼容性,可以使用popcorn.js:


如果您不想这样做,那么只需使用window.setTimeout来显示每个值。

jsonvar[“00-00-02”](保存JSON的变量)应该返回“first one”所以我不知道这里的问题是什么。@MihaRekar问题是给这些文本计时。我仍然不明白。什么时间?@MihaRekar好的,我有一个视频翻译,我想在特定的时间显示每个文本。作为旁注;看看momentjs.com。在Javascript中处理日期非常有用。你没有解决我的问题,我想在指定的时间显示每个文本。你必须为你的问题添加更多细节。它没有说明任何关于视频的内容。但是,请看一看。它完全符合你的要求。谢谢你,但是我怎么能得到这样的时间,我搜索了一下。但是我什么都不懂!是什么脚本生成了你的页面或ajax调用?大多数语言可以将标准的“人类”时间转换为UNIX时间没有语言,我从SQL Server数据库中存储的数据中获取时间,但是如果你告诉我怎么做,我可以更改时间列?我认为这应该可以做到LOL希腊文-这很有趣-只有极客才行