How to refresh or reload a page with timeout in javascript or jquery

梦想的初衷 提交于 2020-01-06 17:46:08

问题


How can I refresh or reload a page with timeout in javascript or jquery only when

  • a specific class is visible or
  • img is not found

i found this https://stackoverflow.com/a/14787543/7051635 but there is no timeout and when i'm in front of an omg not found, nothing happened.


回答1:


Use the setTimeout() global method to create a timer. This will reload the page after 5000 milliseconds (5 seconds):

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

This code can be added to any event you need it to. So, for example:

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>


来源:https://stackoverflow.com/questions/42013358/how-to-refresh-or-reload-a-page-with-timeout-in-javascript-or-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!