且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在JavaScript或jQuery中刷新或重新加载带有超时的页面

更新时间:2021-07-25 04:49:17

使用

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>