使用js定時跳轉頁面

html代碼

<DOCTYPE HTML>
<html>
<head> 
<title>js進階篇</title>
<script src="js/jstestTwo.js"></script>
</head>
<body>
<!--設置一個button , 點擊後出現定時跳轉-->
<input type="button" id="countdown" value="倒計時跳轉" onclick="countDown()" /> 
</body>
</html>

jstestTwo.js

function countDownDate(){
/*通過id獲取html的button的value值*/
    var butValue = document.getElementById("countdown").value;
  /**如果值不爲1(可能是非數字,可能是數字但是不等於1)**/
    if((butValue*1) != 1){
        /*如果是數字,則減一*/
        if(!isNaN(butValue)){
            /*這裏要改變的是html裏面的countdown值,所以使用getelementByID,如果使用butValue,則無法改變html裏面的值,下同*/
            document.getElementById("countdown").value = ((butValue*1)-1); 
        }else {
        /*如果不是數字,給值賦值整數5*/
            document.getElementById("countdown").value = 1*5;
        }
    }else{
  /**如果值等於1,則賦值 跳轉中 給值**/
        document.getElementById("countdown").value = "跳轉中";
        /*同時跳轉到百度首頁*/
        window.location.href = "http://www.baidu.com";
    }
}
/*定時每1秒執行一次countDownDate()方法*/
function countDown() { 
        setInterval(countDownDate,1000);
}
發佈了51 篇原創文章 · 獲贊 19 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章