1- 執行一次(延時定時器)
var t1 = window.setTimeout(function() {
console.log('1秒鍾之后執行了')
},1000)
window.clearTimeout(t1) // 去除定時器
2- 重復執行(間歇定時器)
var t2 = window.setInterval(function() {
console.log('每隔1秒鍾執行一次')
},1000)
window.clearInterval(t2) // 去除定時器
js 刷新當前頁面
本文為大家介紹三種 js 刷新當前頁面的方法:
- reload() 方法;
- replace() 方法;
- 頁面自動刷新;
方法1:reload() 方法
reload()方法用於刷新當前文檔。
reload() 方法類似於你瀏覽器上的刷新頁面按鈕。
location.reload();
方法2:replace() 方法
replace() 方法可用一個新文檔取代當前文檔。。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鳥教程(runoob.com)</title> <script> function replaceDoc() { window.location.replace("http://www.runoob.com") } </script> </head> <body> <input type="button" value="載入新文檔替換當前頁面" onclick="replaceDoc()"> </body> </html>
方法3:頁面自動刷新
頁面自動刷新:把如下代碼加入<head>區域中
<meta http-equiv="refresh" content="5">
其中5指每隔5秒刷新一次頁面。