【问题标题】:How to refresh or reload a page with timeout in javascript or jquery如何在javascript或jquery中刷新或重新加载超时页面
【发布时间】:2017-06-20 04:22:14
【问题描述】:

如何在javascript或jquery中刷新或重新加载页面超时

  • 特定类可见 或
  • 找不到img

我找到了这个https://stackoverflow.com/a/14787543/7051635,但是没有超时,当我在一个未找到的 omg 面前时,什么也没发生。

【问题讨论】:

    标签: javascript jquery refresh settimeout reload


    【解决方案1】:

    使用 setTimeout() 全局方法来创建计时器。这将在 5000 毫秒(5 秒)后重新加载页面:

     setTimeout(function(){ location.reload(); }, 5000);
    

    此代码可以添加到您需要的任何事件中。所以,例如:

    var theDiv = document.querySelector("div");
    
    // When the div is clicked, wait 5 seconds and then hide it.
    theDiv.addEventListener("click", function(){
       setTimeout(function(){ 
         theDiv.classList.add("hide"); 
       }, 5000);
    });
    .hide { display:none; }
    <div>Now you see me.</div>

    【讨论】:

    • 如果我想向 div 添加特定的类,我该如何使用此代码
      现在你看到我了。
    • 它可以放在任何你能想到的条件中。
    • @ScottMarcus 这就是我现在正在寻找的我了解您的代码。谢谢,您能否编辑您的答案,并在 div 中添加一个类
      Now you see me.
      像这样
      Now you see me.
      跨度>
    • 你不会在 DOM 准备好之前运行代码,你会在 DOMContentLoaded 事件中附加一个事件处理程序
    • 是的,事实上,当我的页面加载时,我可能找不到 img,此时我有一个特定的类出现错误消息,当这个类出现时我想自动刷新我的页面。
    猜你喜欢
    • 2011-02-17
    • 1970-01-01
    • 2011-06-27
    • 2012-12-17
    • 2016-10-28
    • 2014-09-29
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多