几种延时跳转代码

几种延时跳转代码

实现5秒后自动跳转效果有两种方式SetTimeOut与setInterval,详细编写如下,有兴致的朋友可以参考下
代码如下:
SetTimeOut版


<script type="text/javascript">
window.onload = function () {
setTimeout(changeTime, 1000);
}
function changeTime() {
var time = document.getElementById("time").innerHTML;
time = parseInt(time);
time--;
if (time <= 0) {
var url = document.getElementById("url").href;
window.location = url;
} else {
document.getElementById("time").innerHTML= time;
setTimeout(changeTime, 1000);
}
}
</script>

setInterval版

$(function () {
setInterval(function () {
var time = $("#time").text();
time = parseInt(time);
time--;
if (time >0) {
$("#time").text(time);
} else {
window.location = $("#url").attr("href");
}
}, 1000);
});


html 版

代码如下:

<span style="font-size: 18px; color: Red" id="time">5</span>秒钟以后跳转到 <a href="<%=this.url %>"

猜你喜欢

转载自binfengwz.iteye.com/blog/2280352