当前位置: 美高梅棋牌 > 智能硬件 > 正文

Jquery倒数计时开关setTimeout的实例代码

时间:2019-09-04 03:44来源:智能硬件
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; // 倒数十下         $(func

<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预设只执行一次,但我们可以用递归,是它自己启动自己。

复制代码 代码如下:

摘自 徐越的专栏

<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>

编辑:智能硬件 本文来源:Jquery倒数计时开关setTimeout的实例代码

关键词: