5秒后停止Javascript函数/设置.5 秒后模糊()



我试图让一个按钮在 5 秒后失去焦点使用 .Blur() 但是使用 setTimeout 和 setInterval 不适用于我正在使用的代码。

我正在使用 VideoJS 来获取视频中的时间,在 1 到 10 秒之间,ID 为"butt6"的按钮应该更改为正在工作的焦点。
我遇到的问题是 5 秒后失焦

在代码中,我有 1 到 10 秒的时间,我将 setTimeout 设置为 5 秒来测试它是否正常工作,但它不是,目前依靠 elseif .blur() 在 10 秒后失去焦点。

我已经在互联网上搜索,试图找到可能遇到类似问题的其他人,但到目前为止我尝试的所有方法要么没有聚焦按钮,要么根本没有取消焦点。

代码如下:

var myPlayer = document.getElementById('my_video_1');
var myFunc = function(){
    var whereYouAt = myPlayer.currentTime; 
    if (whereYouAt > 1 && whereYouAt <= 10){
       var linkToFocus = document.getElementById('butt6');
       linkToFocus.focus();
       setTimeout(function(){ linktoFocus.blur(); }, 5000);
    }
    elseif (whereYouAt > 11){
    linkToFocus.blur();
}
myPlayer.addEventListener('timeupdate',myFunc,false);
我认为

问题是ifsetTimeout之后继续执行和专注。这应该解决:

var myPlayer = document.getElementById('my_video_1');
var hasFocus = false;
var myFunc = function(){
    var whereYouAt = myPlayer.currentTime; 
    if (whereYouAt > 1 && whereYouAt <= 10 && !hasFocus){
       var linkToFocus = document.getElementById('butt6');
       linkToFocus.focus();
       hasFocus = true;
       setTimeout(function(){ linktoFocus.blur(); }, 5000);
    }
}
myPlayer.addEventListener('timeupdate',myFunc,false);

感谢您的建议Tiago,并为延迟回复表示歉意。
不幸的是,"setTimeout"不起作用,但是使用.blur()我设法将焦点从按钮上移开,并使用CSS中的伪类进行格式化以进行过渡。

我的最终代码如下,供参考。

.btn-sq {
          width: 90px !important;
          height: 90px !important;
          font-size: 14px;
          background-color:rgba(255,255,255,1);
          margin: 5px;
          color:#000;
          white-space: normal;
          -o-transition:color .2s ease-out, background-color .2s ease-in;
          -ms-transition:color .2s ease-out, background-color .2s ease-in;
          -moz-transition:color .2s ease-out, background-color .2s ease-in;
          -webkit-transition:color .2s ease-out, background-color .2s ease-in;
          transition:color .2s ease-out, background-color .2s ease-in;
        }
        .btn-sq:hover {
          width: 90px !important;
          height: 90px !important;
          font-size: 14px;
          background-color:#C10000;
          margin: 5px;
          color:#fff;
          white-space: normal;
          -o-transition:color .2s ease-out, background-color .2s ease-in;
          -ms-transition:color .2s ease-out, background-color .2s ease-in;
          -moz-transition:color .2s ease-out, background-color .2s ease-in;
          -webkit-transition:color .2s ease-out, background-color .2s ease-in;
          transition:color .2s ease-out, background-color .2s ease-in;
        }
        .btn-sq:focus {
          width: 90px !important;
          height: 90px !important;
          font-size: 14px;
          background-color:#C10000;
          margin: 5px;
          color:#fff;
          white-space: normal;
          -o-transition:color .2s ease-out, background-color .2s ease-in;
          -ms-transition:color .2s ease-out, background-color .2s ease-in;
          -moz-transition:color .2s ease-out, background-color .2s ease-in;
          -webkit-transition:color .2s ease-out, background-color .2s ease-in;
          transition:color .2s ease-out, background-color .2s ease-in;
        }       

<script>
    var myPlayer = document.getElementById('my_video_1');
    var myFunc = function(){
        var whereYouAt = myPlayer.currentTime; 
        if (whereYouAt > 1 && whereYouAt <= 10){
           var linkToFocus = document.getElementById('butt1');
           linkToFocus.focus();
           setInterval(function(){ linktoFocus.blur(); }, 4000);
        }
        else if (whereYouAt > 11){
           var linkToFocus = document.getElementById('butt1');
           linkToFocus.blur();
        }
    }
    myPlayer.addEventListener('timeupdate',myFunc,false);
</script>

最新更新