我有一个涉及眨眼检测的 javascript 函数,它会影响视频。基本上,当你眨眼时,从 Youtube 嵌入的视频停止播放,并弹出一个确认对话框,让你继续观看视频或重新加载页面。单击“确定”后,视频应该继续播放,但我希望功能停止(或相机停止工作),这样即使您眨眼也能观看视频直到结束。
我尝试使用该函数,但找不到我的错误。
这里是函数的主要代码:
function blink() {
_blinked = true;
_total += 1;
const now = new Date();
const timeDiff = (now - _start) / 1000; //in s
// get seconds
const seconds = Math.round(timeDiff);
if(confirm(`You lasted ${seconds} seconds without blinking! Click OK to
keep playing or CANCEL to watch full video!`)){}
else window.location.replace("fullvideo.html");
_start = new Date();
if (_timeOut > -1) {
clearTimeout(_timeOut);
}
_timeOut = setTimeout(resetBlink, 500);
}
function resetBlink() {
_blinked = false;
}
let _initialized = false;
一旦我点击“确定”,视频应该继续,但我希望功能停止(或相机停止工作),这样即使您眨眼也能观看视频直到结束。 非常感谢。
最佳答案
最简单的方法是拥有一个由每个函数切换的全局 bool 值。 在这种情况下,我们将其称为status:
var status = false;
function blink() {
if (!status){
status = true; // close function
_blinked = true;
_total += 1;
const now = new Date();
const timeDiff = (now - _start) / 1000; //in s
// get seconds
const seconds = Math.round(timeDiff);
if(confirm(`You lasted ${seconds} seconds without blinking! Click OK to
keep playing or CANCEL to watch full video!`)){
status = false; //open function to run again
}
else window.location.replace("fullvideo.html");
_start = new Date();
if (_timeOut > -1) {
clearTimeout(_timeOut);
}
_timeOut = setTimeout(resetBlink, 500);
}
}
现在您的函数将只运行一次,然后如果您在消息上单击“确定”,它应该能够再次运行。
关于javascript - 在确认对话框中单击 'OK' 后如何停止功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55606331/