javascript实现5秒倒计时并跳转功能

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>倒计时五秒</title> <script> //使用匿名函数方法 function countDown(){ var time = document.getElementById("Time"); //alert(time.innerHTML); //获取到id为time标签中的内容,现进行判断 if(time.innerHTML == 0){ //等于0时清除计时 window.location.href="https://www.baidu.com" ; }else{ time.innerHTML = time.innerHTML-1; } } //1000毫秒调用一次 window.setInterval("countDown()",1000); </script> <style> #Time,#p{ font-size: 100px; text-align: center; } </style> </head> <body> <p>5</p> </body> </html>

再为大家分享两段:

jQuery实现5秒倒计时

<script src="https://www.jb51.net/jquery-1.8.2.min.js"></script> <script type="text/javascript"> var i=5; $(function(){ setTimeout(function(){ window.location.href='${ctx}/'; },5000);//5秒后返回首页 after(); }); //自动刷新页面上的时间 function after(){ $("#time").empty().append(i); i=i-1; setTimeout(function(){ after(); },1000); } </script>

点击按钮出现5秒倒计时js代码

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript" src="https://www.jb51.net/js/jquery.js"></script> </head> <body> <input type="button" value="免费获取验证码" /> <script type="text/javascript"> var countdown=5; function settime(val) { if(countdown != 0){ val.setAttribute("disabled", true); val.value="重新发送(" + countdown + ")"; countdown--; }else { val.removeAttribute("disabled"); val.value="免费获取验证码"; countdown = 5; return;//避免无限循环 } setTimeout(function() { settime(val) },1000) } </script> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/616e38558aed72588ccfeb9e4f88516b.html