javascript
JS实现倒计时三秒钟跳转到新的页面
代碼如下:
<div id="box"><p>頁面在<span id="Os">5</span>s后跳轉</p> </div> <script>var Os=document.getElementById("Os");var num=5;var timer=setInterval(function () {num--;Os.innerText=num;if(num==0){window.location.href="https://www.baidu.com/";}},1000) </script>在js代碼里,我用了setInterval()?方法,此方法可按照指定的周期(以毫秒計)來調用函數或計算表達式。
setInterval(code, milliseconds); (code是一個代碼串,milliseconds(時間),多少時間調用一次) setInterval(function, milliseconds, param1, param2, ...) (param是傳給執行函數的其他參數,根據情況寫)這個方法對函數或代碼串會一直調用,直到?clearInterval()?被調用或窗口被關閉。
clearInterval()?方法
可取消由 setInterval() 函數設定的定時執行操作
<div id="box"><p>頁面在<span id="Os">5</span>s后跳轉</p><button id="btn" onclick="stop()">停止</button> </div> <script>var Os=document.getElementById("Os");var num=5;var timer=setInterval(function () {num--;Os.innerText=num;if(num==0){window.location.href="https://www.baidu.com/";}},1000)function stop() {clearInterval(timer);} </script>上個代碼,加了個停止。
若你只想執行一次那就用,setTimeout() 方法,和setInterval() 方法使用方法一樣,這個只執行一次
頁面跳轉的方法:
window.location.href="/url" 當前頁面打開URL頁面?? ?
還有好多種跳轉的方法:self.location.href="/url" 當前頁面打開URL頁面
??????????????????????????????????????? location.href="/url" 當前頁面打開URL頁面
??????????????????????????????????????? this.location.href="/url" 當前頁面打開URL頁面
??????????????????????????????????????? parent.location.href="/url" 在父頁面打開新頁面
??????????????????????????????????????? top.location.href="/url" 在頂層頁面打開新頁面
原文鏈接:https://blog.csdn.net/weixin_43937528/article/details/89336188
總結
以上是生活随笔為你收集整理的JS实现倒计时三秒钟跳转到新的页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: thinkphp5将时间戳直接转换成时间
- 下一篇: 中国陆军入列新锐坦克,这些技术独步全球