如何在点击确认对话框的“确定”后停止函数?

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

一旦我点击“确定”,视频应该继续播放,但我希望函数停止(或者摄像头停止工作),这样即使你眨眼也能继续观看到视频结束。非常感谢你。


回答:

最简单的方法是使用一个全局布尔值,由每个函数切换。我们在这里称之为status

var status = false;function blink() {    if (!status){        status = true; // 关闭函数        _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; // 重新开启函数运行        }        else    window.location.replace("fullvideo.html");        _start = new Date();        if (_timeOut > -1) {            clearTimeout(_timeOut);        }        _timeOut = setTimeout(resetBlink, 500);    }  }

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注