Javascript 如何使计时器每秒都发痒,并使其在视频转发或倒转时跳跃?

Javascript 如何使计时器每秒都发痒,并使其在视频转发或倒转时跳跃?,javascript,jquery,html,timer,html5-video,Javascript,Jquery,Html,Timer,Html5 Video,我正在尝试制作一个计时器,它可以间接地与视频同步。当点击starttimer时,它应该会启动我的计时器,并每秒钟挠痒痒一次 过程如下: 1. Start the video 2. At a certain time in video, click to start the timer 3. Timer starts from 00:00:00 and should tickle each second. 4. If the video is forwarded by `n` seconds ti

我正在尝试制作一个计时器,它可以间接地与视频同步。当点击
starttimer
时,它应该会启动我的计时器,并每秒钟挠痒痒一次

过程如下:

1. Start the video
2. At a certain time in video, click to start the timer
3. Timer starts from 00:00:00 and should tickle each second.
4. If the video is forwarded by `n` seconds timer should be 'timer+n` seconds. Same for the case, when video is rewinded - `timer-n'
但是我的计时器,不能正常工作。它工作正常,当我启动计时器时,但当我向前移动
n
秒时,它有时会移动
n
,有时移动
n+1
n+2
,当我向后移动
n
时,它会自动返回

我就是不能理解正确的逻辑

单击开始菜单时调用:(从00:00:00开始计时)

当视频被转发或倒回时:(我还有一个控件,通过按下shift+r和shift+l,我可以将视频向前移动3秒或向后移动3秒。我希望这相当于
查找

var lastCurrentTime=0;
$('#match')。在('seek',函数(事件)上{
var差=0;
var newCurrentTime=$('#match').get(0).currentTime;
如果(newCurrentTime>lastCurrentTime){
差异=newCurrentTime-lastCurrentTime;
玩=假;
mtimer=mtimer+差值;
$('#starttimer').html(getHHMMSS(mtimer));
玩=真;
}否则{
差异=lastCurrentTime-newCurrentTime;
玩=假;
mtimer=mtimer-差异;
控制台日志(“差异:+差异);
玩=真;
if(mtimer)
  • 设置offfset
  • 使用偏移量前后移动mtimer
  • 查找时的clearinterval
  • starttimer
    功能:

     $('#starttimer').click(function() { // Starts the clock
                                    playing = true;
    
                                    if(!timerstarted) {
                                        offset = $('#match').get(0).currentTime;
                                        timerv = setInterval(function() {
                                            var newCurrentTime = $('#match').get(0).currentTime;
    
    
                                        if(playing) {                                               
                                                mtimer++;
                                            $('#starttimer').html(getHHMMSS(mtimer));
                                        }                                                                              
    
                                                //$('#starttimer').html(getHHMMSS(mtimer));
                                            } , 1000 ); 
                                        timerstarted = true;
                                    }
                                });
    
    $('#match').on('seeking',function(event) {
                                        playing = true;
                                        if(timerstarted) {
                                            clearInterval(timerv);
                                            var newCurrentTime = $('#match').get(0).currentTime;
                                            mtimer = newCurrentTime - offset;
                                           if(mtimer < 0) {
                                                       mtimer = 0;  
                                                       offset = 0;
                                                       $('#starttimer').html(getHHMMSS(mtimer));
                                                       console.log("playing : " + playing);
                                           }
                                           timerv = setInterval(function() {                                       
                                               if(playing) { 
                                                       console.log("Inside playing...");
                                                       mtimer++;
                                                   $('#starttimer').html(getHHMMSS(mtimer));
                                               }                                                                              
                                                   /*if(playing) {
                                                       if(timerset === true && $('#timeentered').val() !== '') {
                                                           mtimer = $('#timeentered').val();
                                                           timerset = false;
                                                       }
                                                      mtimer++;
                                                   }*/
                                                   //$('#starttimer').html(getHHMMSS(mtimer));
                                               } , 1000 ); 
                                           lastCurrentTime = newCurrentTime;
                                        } 
                });
    
    搜索
    功能:

     $('#starttimer').click(function() { // Starts the clock
                                    playing = true;
    
                                    if(!timerstarted) {
                                        offset = $('#match').get(0).currentTime;
                                        timerv = setInterval(function() {
                                            var newCurrentTime = $('#match').get(0).currentTime;
    
    
                                        if(playing) {                                               
                                                mtimer++;
                                            $('#starttimer').html(getHHMMSS(mtimer));
                                        }                                                                              
    
                                                //$('#starttimer').html(getHHMMSS(mtimer));
                                            } , 1000 ); 
                                        timerstarted = true;
                                    }
                                });
    
    $('#match').on('seeking',function(event) {
                                        playing = true;
                                        if(timerstarted) {
                                            clearInterval(timerv);
                                            var newCurrentTime = $('#match').get(0).currentTime;
                                            mtimer = newCurrentTime - offset;
                                           if(mtimer < 0) {
                                                       mtimer = 0;  
                                                       offset = 0;
                                                       $('#starttimer').html(getHHMMSS(mtimer));
                                                       console.log("playing : " + playing);
                                           }
                                           timerv = setInterval(function() {                                       
                                               if(playing) { 
                                                       console.log("Inside playing...");
                                                       mtimer++;
                                                   $('#starttimer').html(getHHMMSS(mtimer));
                                               }                                                                              
                                                   /*if(playing) {
                                                       if(timerset === true && $('#timeentered').val() !== '') {
                                                           mtimer = $('#timeentered').val();
                                                           timerset = false;
                                                       }
                                                      mtimer++;
                                                   }*/
                                                   //$('#starttimer').html(getHHMMSS(mtimer));
                                               } , 1000 ); 
                                           lastCurrentTime = newCurrentTime;
                                        } 
                });
    
    $('#match')。在('seeking',函数(事件)上{
    玩=真;
    如果(计时器开始){
    清除间隔(timerv);
    var newCurrentTime=$('#match').get(0).currentTime;
    mtimer=newCurrentTime-偏移量;
    如果(mtimer<0){
    mtimer=0;
    偏移量=0;
    $('#starttimer').html(getHHMMSS(mtimer));
    console.log(“playing:+playing”);
    }
    timerv=setInterval(函数(){
    如果(玩){
    console.log(“内部播放…”);
    mtimer++;
    $('#starttimer').html(getHHMMSS(mtimer));
    }                                                                              
    /*如果(玩){
    if(timerset==true&&$('#timeentered').val(){
    mtimer=$('#timeentered').val();
    timerset=false;
    }
    mtimer++;
    }*/
    //$('#starttimer').html(getHHMMSS(mtimer));
    } , 1000 ); 
    lastCurrentTime=newCurrentTime;
    } 
    });
    
    启动计时器时,您需要同步两个计时器变量。
    mtimer
    变量在启动计时器时开始计算秒数,但
    lastCurrentTime
    设置为零,直到您第一次“搜索”某个方向的视频为止。这将导致计时中断

    假设您在视频播放一分钟后启动计时器。一旦您观看了一分钟的视频,
    mtimer
    为60秒,视频计时器为120秒,
    lastCurrentTime
    仍为零。如果我将视频回滚90秒,
    mtimer
    应在30秒前变为负数,但根据您的code,
    mtimer
    将设置为正30

    试试这个:

    var mtimer = 0;
    var lastCurrentTime = 0;
    $('#starttimer').click(function() { // Starts the clock
                                playing = true;
    
                                if(!timerstarted) {
    
                                    //Set the timer before starting the interval
                                    //Gives you one second with the timer at zero before the first interval runs
                                    $('#starttimer').html(getHHMMSS(mtimer));
    
                                    //Set lastCurrentTime to the video time.
                                    lastCurrentTime = $('#match').get(0).currentTime;
                                    setInterval(function() {
                                        if(playing) {                                               
                                            //Keeps both timers synched as the movie plays.
                                            mtimer++;
                                            lastCurrentTime++;
                                            $('#starttimer').html(getHHMMSS(mtimer));
                                    }
                                        } , 1000 ); 
                                    timerstarted = true;
                                }
                            });
    
    现在,您的当前搜索功能应该可以工作了。如果
    newCurrentTime
    大于
    lastCurrentTime
    mtimer
    将按此差值递增。如果相反,则
    mtimer
    将按此差值递减。在我上面提到的情况下,
    mtimer
    应已消失负30秒,
    mtimer
    将设置为零。根据您的代码,我假设如果
    mtimer
    低于零,您希望重置计时器


    希望这能满足您的需要。

    所以您想在
    setInterval
    中发出
    timeupdate
    事件?您必须在视频向前和向后操作的句柄/回调中执行加减操作。否则除了setInterval之外,没有ant最佳方法。@sunilsharma这会有什么影响?我仍在
    se上执行此操作eking
    event这很好,w