I am trying to make a timer which indirectly syncs with the video. When starttimer
is clicked, it should starts my timer and tickle each second.
Here is the process:
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'
But my timer, is not functioning properly. It works fine, when I start the timer but when I forward by n
seconds, it sometimes goes by n
and sometimes by n+1
or n+2
and when I rewind by n
it goes back at its own will.
I just cannot get the logic correct.
Called when starttimer
is clicked: (It starts the clock from 00:00:00)
var mtimer = 0;
$('#starttimer').click(function() { // Starts the clock
playing = true;
if(!timerstarted) {
setInterval(function() {
if(playing) {
mtimer++;
$('#starttimer').html(getHHMMSS(mtimer));
}
} , 1000 );
timerstarted = true;
}
});
When video is forwarded or rewinded: (I also have a control, wherein I could move the video forward by 3 seconds or back by 3 seconds by pressing shift+r and shift+l. I hope it is equivalent to seeking
)
var lastCurrentTime = 0;
$('#match').on('seeking',function(event) {
var difference = 0;
var newCurrentTime = $('#match').get(0).currentTime;
if(newCurrentTime > lastCurrentTime) {
difference = newCurrentTime - lastCurrentTime;
playing = false;
mtimer = mtimer + difference;
$('#starttimer').html(getHHMMSS(mtimer));
playing = true;
}else {
difference = lastCurrentTime - newCurrentTime;
playing = false;
mtimer = mtimer - difference;
console.log("Difference : " + difference);
playing = true;
if(mtimer <= 0) {
mtimer = 0;
$('#starttimer').html(getHHMMSS(mtimer));
}
}
lastCurrentTime = newCurrentTime;
});
starttimer
function:
$('#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;
}
});
seeking
function:
$('#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;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With