使用javascript检查时间变量落入的范围

时间:2013-03-10 13:26:38

标签: jquery javascript-events javascript

我想取一个特定的时间值(由html5 htmlmedia timerange.end以秒为单位),并检查它在我已经拥有的那些时间列表中的位置

00:00:00.00
00:00:22.00
00:01:21.00
00:01:36.00
00:01:51.00
00:02:06.00
00:02:21.00
00:02:36.00

我在捕获程序的文本文件服务器端硬编码了这些值。 我想检查并查看它(video_time)所在的两个值之间,如果src与时间段不对应,则从那里更改srame中的src,在上面的列表中有n次并且对应n它们之间有-1个槽,src将通过/Slide [n-1 ].html命名为/Slide0.html  到目前为止,iv创建了一些逻辑来检查时间,如果值大于15秒,则将持有者逻辑更改为iframe的src,并且某些callBack定期调用,

[编辑:删除了一些不相干的东西] 这是问题,

function category() {
    var played_ob =video_time_played() ;
    if ( played_ob  > 0 )
      var played_num = played_ob;
    else
     var played_num = 0;
    var tes = 15;
    if ( played_num > tes)
     document.getElementsByTagName("iframe")[0].src="/static/Slide1.html";
    else
     console.log ("meh");
};

上面我想检查它落在哪个时间范围并设置正确的iframe。这就是我需要帮助的,我想做的是如下

/* PSEUDOCODE OF WHAT I WISH TO ACHIEVE BUT DONT KNOW HOW TO DO IT ENTIRELY.*/
 function category (){
     var time_spent = video_time_played_so_far_in_seconds(); 
       /* this part i dont know, especially how to generate the list from a text file serverside */
     categories_list = [list of consecutive times i showed above in HH:MM:SS.00]

     var which_category = function check_in_between_which_two_times_var_lies () {
        /***********  i DONT know how to code this part either, *******/
        the_category_found_as_an_integer_between_0=>n-1 = ? 
        var category = the_category_found_as_an_integer_between_0=>n-1 ;
        return category  ;
       }

     function which_new_src (which_category) {
      /* code to select new src */
         var new_src = "/static/Slide"+which_category".html";
         return new_src;
     }

     function change_src_of_iframe () {
         var relevant_iframe = document.getElementsByTagName("iframe")[0];

         change_src_of_iframe () {
         var new_src = which_new_src(which_category);
         relevant_iframe..src= new_src ;
         }

1 个答案:

答案 0 :(得分:1)

这就是我最终做的事情:

将带有HH:MM:SS的有序数组转换为有序的秒数组:

function to_seconds (capture){
console.log(capture.length);
var clean = new Array() ;
for ( var i = 0, len = capture.length; i<len; i++ ) {
     if (capture[i] ) 
       clean.push(capture[i]);
    } 

console.log(clean.length);
var ready = clean ;

var times = new Array() ;
   for ( var i = 0, len = ready.length; i<len; i++ ) {

       var obj =  ready[i].split(':'); 
       var seconds =obj[0]*3600+obj[1]*60+obj[2]*1;
       times.push(seconds) ;
   }
console.log(times.length);
return times ; 
}

然后检查到目前为止播放时间的插槽,

function find_category_number (time,list) {
 var number = 0;
 for ( var i = 0, len = list.length; i<len; i++ ) {
 if ( time > list[i] && time < list[i+1] )
    number= i;
 }

return number ;
}

最后生成一个新的src值:

function find_new_src (category_number) {
var src = "/static/Slide"+category_number+".html" ;
return src ;
}

这就是我最需要做的事情,猜测将问题分解成小模块化部分有点帮助,我试图同时做所有逻辑,我的大脑冻结了。