第一种使用SetInterval:
$(function () {
setInterval(ChangeTime, 1000);
});
function ChangeTime() {
var time;
time = $("#time").text();
time = parseInt(time);
time--;
if (time <= 0) {
window.location.href = "/Home/Index";
} else {
$("#time").text(time);
}
}
第二种使用SetTimeout:
$(function () {
setTimeout(ChangeTime, 1000);
});
function ChangeTime() {
var time;
time = $("#time").text();
time = parseInt(time);
time--;
if (time <= 0) {
window.location.href = "/Home/Index";
} else {
$("#time").text(time);
setTimeout(ChangeTime, 1000);
}
}
<div>你访问的页面出现异常,<span id="time">5</span>秒钟自动跳到首页</div>