如何使计时器每秒挠痒痒,并在转发或倒带视频时使其跳跃
How to make the timer tickle each second and make it jump when video is forwarded or rewinded?
我正在尝试制作一个间接与视频同步的计时器。单击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
时,它会自行返回。
我只是无法正确理解逻辑。
单击starttimer
时调用:(它从 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;
}
});
转发或快退视频时:(我还有一个控件,其中我可以通过按 shift+r 和 shift+l 将视频向前移动 3 秒或向后移动 3 秒。我希望它相当于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;
});
- 设置偏移量
- 使用偏移来回移动 mtimer
- 查找时的清除间隔
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;
}
});
seeking
功能:
$('#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;
}
});
启动计时器时,您需要同步两个计时器变量。当您启动计时器时,mtimer
变量开始计算秒数,但lastCurrentTime
设置为零,直到您第一次"搜索"视频一个方向或另一个方向。这将打乱时机。
假设您在视频中一分钟启动计时器。观看视频一分钟后,mtimer
为 60 秒,视频计时器为 120 秒,lastCurrentTime
仍为零。如果我将视频回滚 90 秒,mtimer
应该负 30 秒,但是,根据您的代码,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
应该负三十秒时,mtimer
将被设置为零。根据您的代码,我假设您希望在计时器低于零时重置mtimer
。
希望这符合您的需求。
相关文章:
- 视频HTML没有'无法在Internet Explorer 11上工作
- 播放当前视频时如何停止其他视频?JavaScript
- 使用Facebook live API创建实时视频对象时的隐私设置
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- Brightcove获取/显示HTML中的当前视频标题和描述
- 使用带括号的图像URL作为jQuery的背景
- 如何让iPhone用户在我的网站上观看带字幕的视频
- 在Javascript中实现倒带功能
- 为什么我的音频没有倒带
- 如何将变量递增 1 以限制,然后倒带并重复递增 - jQuery
- ff和倒带,而不使用currentTime属性
- 我如何创建一个YouTube视频的倒带功能
- HTML5视频暂停和倒带
- 滑块,在第5张幻灯片之后,倒带需要更改以继续前进
- 如何在html5-video中允许倒带但禁用快进
- 带进度条的base64视频上传
- 任何方式有一个15秒的倒带按钮在jPlayer jquery插件
- 如何使计时器每秒挠痒痒,并在转发或倒带视频时使其跳跃
- 播放带字幕的视频
- 如果视频=持续时间,停止并倒带