页面定时跳转功能,比如设定2022年5月1日中午12点21分32秒跳转去baidu首页
<p><span id="days"></span>天<span id="hours"></span>小时<span id="minutes"></span>分<span id="seconds"></span>秒后自动跳转到百度</p>
<!-- js部分 -->
var currentTime = Date.now()
var redirectTime = new Date("2022-05-01 12:21:32")
var minusTime = redirectTime - currentTime
var daysValue = document.getElementById("days")
var hoursValue = document.getElementById("hours")
var minutesValue = document.getElementById("minutes")
var secondsValue = document.getElementById("seconds")
function countDown() {
if (minusTime <= 0) {
location.href = "https://www.baidu.com/"
clearInterval(interval)
} else {
var days = parseInt(minusTime / 1000 / 60 / 60 / 24, 10)
var hours = parseInt(minusTime / 1000 / 60 / 60 % 24, 10)
var minutes = parseInt(minusTime / 1000 / 60 % 60, 10)
var seconds = parseInt(minusTime / 1000 % 60, 10)
if (String(days).length < 2) {
days = "0" + days
}
if (String(hours).length < 2) {
hours = "0" + hours
}
if (String(minutes).length < 2) {
minutes = "0" + minutes
}
if (String(seconds).length < 2) {
seconds = "0" + seconds
}
daysValue.innerHTML = days
hoursValue.innerHTML = hours
minutesValue.innerHTML = minutes
secondsValue.innerHTML = seconds
minusTime = minusTime - 1000
}
}
countDown()
var interval = setInterval(countDown, 1000)
|