使用jquery重置每个文档点击的超时

Kev*_*Moe 2 html javascript jquery settimeout

我正在努力让timeout计时器在每次交互时重置document click。似乎clearTimeout不起作用。

jQuery:

$(document).click(function(e){
  $("#vidBack").hide();
  $(".mainer").show();

  function stopTimer() {
    clearTimeout(timer);
  }

  var timer =  setTimeout(function(){
        $(".bury").hide();
        $(".mainer").hide();
        $("#meetSub").hide();
        $("#leaguesSub").hide();
        $("#memberSub").hide();
        $(".bury").hide();
        $("#welcomeSub").show();
        $("#vidBack").show();
        $("#vida").prop("volume", 0);       
  }, 10000);
});
Run Code Online (Sandbox Code Playgroud)

Anu*_*ava 5

将您的代码重构为如下所示:

var timer;

function resetTimer() {
  if(timer)
    clearTimeout(timer);
  
  $("#vidBack").hide();
  $(".mainer").show();
  
  timer = setTimeout(function() {
    $(".mainer").hide();
    $("#vidBack").show();
  },  5000);
  
}

$(document).click(resetTimer);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="mainer">mainer</div>
<div id="vidBack">vidBack</div>
Run Code Online (Sandbox Code Playgroud)