歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

jQuery倒數計時按鈕—setTimeout

<head runat="server">
    <title></title>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        var timeout;
        var count = 10; // 倒數十下

        $(function() {
            timeout = setTimeout(BtnCount, 1000); // 1s執行一次BtnCount
        });

        BtnCount = function() {
           

               // 啟動按鈕

               if (count == 0) {
                $('#btnSubmit').attr("disabled", "");
                $('#btnSubmit').val("確定");
                clearTimeout(timeout);           // 可取消由 setTimeout() 方法設置的 timeout
            }
            else {
                count--;
                $('#btnSubmit').val("確(" + count.toString() + ")定");
                setTimeout(BtnCount, 1000); 
            }
        };
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnSubmit" runat="server" Text="確定" Enabled="false"
            onclick="btnSubmit_Click"/>
    </div>
    </form>
</body>

 

setTimeout預設只執行一次,但我們可以用遞歸,是它自己啟動自己。

Copyright © Linux教程網 All Rights Reserved