【问题标题】:Reveal div if no click on a certain other div for more than x seconds如果超过 x 秒没有点击某个其他 div,则显示 div
【发布时间】:2016-02-27 01:04:46
【问题描述】:

我目前在 javascript 中有一个代码,目前可以很好地满足我的需求:

  • 警报消息在 7 秒后首次出现
  • 当点击或触摸 div TARGET 时移除警报

但我没有做到这一点:一旦用户点击了一个目标(这已经是可操作的),但我希望在之后再次出现警报消息,每次用户没有点击时。目标 div 超过 5 秒

我重新措辞更清楚:我希望消息在 7 秒后首先出现,然后每次用户单击.target 时,消息应该消失。但是,如果他在 .target 上点击的时间不超过 5 秒,该消息应该会重新出现

如何做到这一点?

html

<section id="alert-msg" style="visibility: hidden"> 
  <div>my message</div>
</section>

Javascript

//1. message only appear after 7 sec
function showIt2() {
    document.getElementById("alert-msg").style.visibility = "visible";
  }
  setTimeout(showIt2, 7000); 

// 2. alert message disappear when .target div is clicked
  $('.target').on('click touchstart', function(){
    $("#alert-msg").remove();
  });

这是一个 jsfiddle:https://jsfiddle.net/111994re/3/

【问题讨论】:

  • 你能用你目前拥有的东西设置一个 jsfiddle
  • id="alert-msg"$("#alert") 是不是错字?
  • 是的错字我会更正它
  • 是的,简化测试用例的 jsFiddle 会很棒。这个错误可能出现在很多不同的地方。
  • 这不是一个错误,我没有设法做我提到的最后一件事。

标签: javascript jquery


【解决方案1】:

查看此版本:https://jsfiddle.net/111994re/7/

我在你的 onclick 中添加了 setTimeout 我已将可见性设置为隐藏而不是删除它(删除它你不能取消隐藏它,你必须重新添加)

//1. message only appear after 7 sec
function showIt2() {
    document.getElementById("alert-msg").style.visibility = "visible";
  }
  setTimeout(showIt2, 7000); 

// 2. alert message disappear when .target div is clicked
  $('.target').on('click touchstart', function(){
    document.getElementById("alert-msg").style.visibility = "hidden";
    setTimeout(showIt2, 5000);
  });

【讨论】:

  • 把我的问题改得更清楚了。如果用户没有点击任何.target,我需要每隔 5 秒提醒一次重新出现
  • 无论用户何时放开鼠标,都会在点击后显示 5 秒。例如:如果他们单击鼠标并按住它,它仍会在 5 秒后重新出现。
  • @jnoreiga,他们必须单击具有 target 类的元素,这是消息消失的唯一一次,是的,它在 touchstart
【解决方案2】:

那里有些打字错误

//1. message only appear after 7 sec
function showIt2() {
    document.getElementById("alert-msg").style.visibility = "visible";
  }
  setTimeout(showIt2, 7000); 

// 2. alert message disappear when .target div is clicked
  $('.target').on('click touchstart', function(){
    $("#alert-msg").remove();
});
<section id="alert-msg" style="visibility: hidden"> 
  <div>my message</div>
</section>

【讨论】:

    【解决方案3】:

    https://jsfiddle.net/111994re/10/

       //1. message only appear after 7 sec
    function showIt2() {
        $("#alert-msg").show();
      }
      setTimeout(showIt2, 7000); 
    
    // 2. alert message disappear when .target div is clicked
      $('.target').on('mousedown touchstart', function(){
      $("#alert-msg").hide(); 
        clearTimeout(this.downTimer);
    
      }).on("mouseup touchstop", function(e) {
        this.downTimer = setTimeout(function() {
    
              showIt2();
        }, 5000);
    });
    

    【讨论】:

    • 现在即使我点击.target,消息也不会消失
    • 您在编辑之前的原始请求声明您希望它在单击目标超过 5 秒时删除警报。如果您按下目标超过 5 秒,我的示例将删除它。您的问题令人困惑。
    • 我仍然想保留这样的方法:如果用户点击 .target 消息应该消失。 (在我的初始代码中)但它不再适用于您的代码。我想要两样东西
    • 好的,我更新了我的解决方案。它将在 7 秒后显示该消息。然后当用户单击目标时它将隐藏它。用户停止点击目标 5 秒后,它会重新出现。
    • 消息永远不会出现(他最初应该在 7 秒后)
    【解决方案4】:

    好的,现在,警报会在 x 秒后出现。然后,如果用户单击目标,计时器会重置,并且在 x 秒后无限期地再次出现警报。

    <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
    
    <section id="alert-msg"> 
        <div style="display:none;">Alert!</div>
    </section>
    
    <div class="target">Target</div>
    
    <script type="text/javascript">
    //1. message only appear after 7 sec
    function showIt2() {
        if( $('#alert-msg') ){
            $('#alert-msg div').fadeOut();
            $('#alert-msg div').fadeIn();
        }
      }
      var show = setInterval(showIt2, 5000); 
    
    // 2. alert message disappear when .target div is clicked
      $('.target').on('click touchstart', function(){
        $("#alert-msg div").hide();
        clearInterval(show);
        show = setInterval(showIt2, 5000); 
      });
    </script>
    

    【讨论】:

    • 把我的问题改得更清楚了。如果用户没有点击任何.target,我需要每隔 5 秒提醒一次重新出现
    • 啊-好的。我会重写我的答案
    • 好的,已经更新了。你必须使用 setInterval 和 clearInterval
    • 在这里尝试:jsfiddle.net/111994re/6。这是一件很奇怪的事情。你好,往下,往下,往下……:)
    • 是的,你想让它消失然后重新出现吗?
    猜你喜欢
    • 2019-06-03
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2014-04-23
    • 2012-05-14
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多