溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用javaScript代碼實現支付10秒倒計時效果

發布時間:2022-10-21 16:40:27 來源:億速云 閱讀:169 作者:iii 欄目:編程語言

這篇文章主要介紹了如何使用javaScript代碼實現支付10秒倒計時效果的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇如何使用javaScript代碼實現支付10秒倒計時效果文章都會有所收獲,下面我們一起來看看吧。

效果圖如下:

如何使用javaScript代碼實現支付10秒倒計時效果

首先需要兩個html文件,在兩個文件中利用html和css分別寫好初始頁面效果,在這里就不多說啦,具體可以看下面的代碼

讓我們來談談js需要做出的效果:

1、在頁面1中點擊支付要跳轉到另一個文件中
2、剛進入頁面2時要開始計時10秒,計時結束后返回頁面1
3、點擊頁面2的立即返回能夠返回到頁面1

這就是我們需要做的效果

那我們要如何實現在兩個頁面之間的跳轉呢?

=> 利用onclicklocation.href="url" rel="external nofollow" ,在鼠標點擊時改變location.href
(此處的url是指你所存放的另一個html文件的位置)

計時效果就很簡單啦,利用setInterval使元素的innerText改變就可以了,當數字等于0時,同樣改變location,使其頁面跳轉

代碼如下:

頁面1:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #btn{
            display: block;
            margin:130px auto;
            width: 300px;
            height: 100px;
            font-size:30px;
        }
    </style>
</head>
<body>
    <button id="btn">支付</button>
    <script>
        let btn=document.getElementById("btn");
        
        btn.onclick=function(){
            let con=window.confirm("您確定嗎?");
            if(con){
                location.href='./支付.html';
            }
        }
    </script>
</body>
</html>

頁面2:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #spa {
            font-size: 20px;
            color: red;
        }

        #total {
            width: 200px;
            height: 200px;
            background-color: rgba(169, 169, 169, 0.315);
            margin: 40px auto;
            border-radius: 20px;
            padding: 20px;
            position: flex;
            flex-direction: column;
            text-align: center;
        }

        #total h4 {
            padding-top: 20px;
        }

        #total button {
            margin-top: 30px
        }
    </style>
</head>

<body>
    <div id="total">
        <h4>恭喜您,支付成功!</h4>
        <div>
            <span id="spa">10</span>
            <span>秒后自動返回首頁</span>
        </div>
        <button id="btn">立即返回</button>
    </div>
    <script>
        var spa = document.getElementById("spa");
        let t = 10;
        setInterval(() => {
            t--;
            spa.innerText = t;
            if (t == 0) {
                location.href = "./支付10秒鐘.html";
            }
        }, 400);
        
        var btn=document.getElementById("btn");
        btn.onclick=function(){
            location.href="./支付10秒鐘.html" rel="external nofollow" 
        }
    </script>
</body>

</html>

關于“如何使用javaScript代碼實現支付10秒倒計時效果”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“如何使用javaScript代碼實現支付10秒倒計時效果”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女