<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="showtimes">加载中。。。</div> <script type="text/javascript"> //设定倒数秒数 var t = 10; //显示倒数秒数 function showTime(){ t -= 1; document.getElementById(‘showtimes‘).innerHTML= t; if(t==0){ location.href=‘http://www.baidu.com‘; } //每秒执行一次,showTime() setTimeout("showTime()",1000); } //执行showTime() showTime(); </script> </body> </html>
效果图:
当倒计时到等于0的时候停止为0:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="showtimes">加载中。。。</div> <script type="text/javascript"> //设定倒数秒数 var t = 10; //显示倒数秒数 function showTime(){ t -= 1; document.getElementById(‘showtimes‘).innerHTML= t; //每秒执行一次,showTime() s=setTimeout("showTime()",1000); //若倒计时到等于0,就停止 if(t==0){ t = 0; clearTimeout(s); } } //执行showTime() showTime(); </script> </body> </html>