javascript
JS实现倒计时三秒钟跳转到新的页面
代碼如下:
<div id="box"><p>頁(yè)面在<span id="Os">5</span>s后跳轉(zhuǎn)</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()?方法,此方法可按照指定的周期(以毫秒計(jì))來(lái)調(diào)用函數(shù)或計(jì)算表達(dá)式。
setInterval(code, milliseconds); (code是一個(gè)代碼串,milliseconds(時(shí)間),多少時(shí)間調(diào)用一次) setInterval(function, milliseconds, param1, param2, ...) (param是傳給執(zhí)行函數(shù)的其他參數(shù),根據(jù)情況寫(xiě))這個(gè)方法對(duì)函數(shù)或代碼串會(huì)一直調(diào)用,直到?clearInterval()?被調(diào)用或窗口被關(guān)閉。
clearInterval()?方法
可取消由 setInterval() 函數(shù)設(shè)定的定時(shí)執(zhí)行操作
<div id="box"><p>頁(yè)面在<span id="Os">5</span>s后跳轉(zhuǎn)</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>上個(gè)代碼,加了個(gè)停止。
若你只想執(zhí)行一次那就用,setTimeout() 方法,和setInterval() 方法使用方法一樣,這個(gè)只執(zhí)行一次
頁(yè)面跳轉(zhuǎn)的方法:
window.location.href="/url" 當(dāng)前頁(yè)面打開(kāi)URL頁(yè)面?? ?
還有好多種跳轉(zhuǎn)的方法:self.location.href="/url" 當(dāng)前頁(yè)面打開(kāi)URL頁(yè)面
??????????????????????????????????????? location.href="/url" 當(dāng)前頁(yè)面打開(kāi)URL頁(yè)面
??????????????????????????????????????? this.location.href="/url" 當(dāng)前頁(yè)面打開(kāi)URL頁(yè)面
??????????????????????????????????????? parent.location.href="/url" 在父頁(yè)面打開(kāi)新頁(yè)面
??????????????????????????????????????? top.location.href="/url" 在頂層頁(yè)面打開(kāi)新頁(yè)面
原文鏈接:https://blog.csdn.net/weixin_43937528/article/details/89336188
總結(jié)
以上是生活随笔為你收集整理的JS实现倒计时三秒钟跳转到新的页面的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: thinkphp5将时间戳直接转换成时间
- 下一篇: JS 正则表达式(数字、正则)