如何在确认对话框中单击“确定”后停止功能?

问题描述 投票:0回答:1

我有一个涉及眨眼检测的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;

一旦我点击“确定”,视频就会继续,但我希望功能停止(或者相机停止工作),这样即使你眨眼也可以观看视频直到结束。非常感谢你。

javascript algorithm function artificial-intelligence
1个回答
1
投票

最简单的方法是使用每个函数切换的全局布尔值。在这种情况下我们将其称为状态:

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);
    }
  }

现在你的功能只会运行一次,如果你在消息上单击“确定”,它应该能够再运行一次。

© www.soinside.com 2019 - 2024. All rights reserved.