js代碼實現(xiàn)點擊按鈕出現(xiàn)60秒倒計時
比如,一些網(wǎng)站上的手機短信認證的功能,有類似實現(xiàn)點擊按鈕后,倒計時60秒才能再次點擊發(fā)送的效果。
此例子用Javascript實現(xiàn)點擊按鈕后,倒計時60秒才能再次點擊發(fā)送驗證碼的功能。
例子1:Javascript 實現(xiàn) 點擊按鈕 倒計時60秒方可再次點擊發(fā)送的效果
<input type="button" id="btn" value="免費獲取驗證碼" /> <script type="text/javascript"> var wait=60; function time(o) { if (wait == 0) { o.removeAttribute("disabled"); o.value="免費獲取驗證碼"; wait = 60; } else { o.setAttribute("disabled", true); o.value="重新發(fā)送(" + wait + ")"; wait--; setTimeout(function() { time(o) }, 1000) } } document.getElementById("btn").onclick=function(){time(this);} </script>
例子2:點擊按鈕出現(xiàn)60秒倒計時js代碼
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標(biāo)題文檔</title> <script type="text/javascript" src="js/jquery.js"></script> </head> <body> <input type="button" id="btn" value="免費獲取驗證碼" onclick="settime(this)" /> <script type="text/javascript"> var countdown=60; function settime(val) { if (countdown == 0) { val.removeAttribute("disabled"); val.value="免費獲取驗證碼"; countdown = 60; } else { val.setAttribute("disabled", true); val.value="重新發(fā)送(" + countdown + ")"; countdown--; } setTimeout(function() { settime(val) },1000) } </script> </body> </html>
例子3:點擊按鈕后,60秒倒計時后才能繼續(xù)可以點擊,按鈕上還能顯示倒計時
預(yù)想的效果圖:
這是微信公眾平臺上的代碼
function E() { var e = $("#mobile"), t = (new Date).getTime(), n = Math.floor((t - b) / 1e3); g && clearTimeout(g), n >= 60 ? (e.prop("readonly", !1), y = !0, $("#sendmobile").html("發(fā)送驗證碼").attr("disabled", !1).removeClass("btn_disabled")) : (e.prop("readonly", !0), y = !1, $("#sendmobile").attr("disabled", !0).addClass("btn_disabled").html("%s秒后可重發(fā)".sprintf(60 - n)), g = setTimeout(E, 1e3)); } function S() { function e() { if (!y) return; var e = $.trim(n.val()); l.mobile(e) ? t.attr("disabled", !1).removeClass("btn_disabled") : t.attr("disabled", !0).addClass("btn_disabled"); } var t = $("#sendmobile"), n = $("#mobile"); n.keyup(e).blur(e), e(), t.click(function() { var e; t.attr("disabled") !== "disabled" && (e = "+86" + $.trim(n.val()), b = (new Date).getTime(), E(), o.post({ url: w ? "/cgi-bin/formbyskey" : "/acct/formbyticket", data: { form: "mobile", action: "set", f: "json", mobile: e }, mask: !1 }, function(e) { var t = e.BaseResp.Ret; if (t == 0) u.suc("驗證碼已經(jīng)發(fā)送"); else { switch (t) { case -13: u.err("登錄超時,請重新登錄"); break; case -35: u.err("該手機已經(jīng)登記過2次,請使用別的手機號進行用戶信息登記"); break; default: u.err("驗證碼發(fā)送失敗"); } b = 0; } })); }); }
這段代碼小編就是不知道怎么才能調(diào)用到自己的代碼上來,經(jīng)過高人指點有了一些頭尋。
高人的解題思路:如果嚴格的話,這個還要結(jié)合后臺獲取時間的,要不然別人刷新一下就行了。
如果不嚴格,用個cookie也可以。
<script src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></script> <script src="http://yukon12345.com/yukon12345.com/js/jquery.cookie.js"></script> <script> time1=$.cookie("time1")||60; time2=$.cookie("time2")||60; dis1=$.cookie("dis1") dis2=$.cookie("dis2") function countDown($obj){ var time; if($obj.attr("id")=="b1") { time=--time1; $.cookie("time1",time,{"expires":1}); if(time<=0){ time1=60; $obj[0].disabled=!$obj[0].disabled clearInterval(inter1) $obj.text("點擊發(fā)送") $.cookie("dis1","") return } } if($obj.attr("id")=="b2") { time=--time2; $.cookie("time2",time,{"expires":1}); if(time<=0){ time1=60; $obj[0].disabled=!$obj[0].disabled clearInterval(inter2) $obj.text("點擊發(fā)送") $.cookie("dis2","") return } } $obj.text(time+"秒后重新發(fā)送") } $(function(){ if(dis1="dis"){ $("#b1")[0].disabled='disabled' inter1=setInterval(function(){countDown($("#b1"))},1000) } if(dis2="dis"){ $("#b2")[0].disabled='disabled' inter2=setInterval(function(){countDown($("#b2"))},1000) } $(".cd").bind("click",function(){ $this=$(this); //沒有被禁用時禁用并執(zhí)行倒計時 if(!$this[0].disabled){ $this[0].disabled='disabled'; if($this.attr("id")=="b1"){ $.cookie("dis1","dis",{"expires":1}) inter1=setInterval(function(){countDown($this)},1000) } if($this.attr("id")=="b2"){ $.cookie("dis2","dis",{"expires":1}) inter2=setInterval(function(){countDown($this)},1000) } } }) }) </script> <button id="b1" class="cd" >點擊發(fā)送</button><br> <button id="b2" class="cd" >點擊發(fā)送</button><br>
更多關(guān)于倒計時的文章請查看專題: 《倒計時功能》
大家通過這三個例子講解有沒有點思路了,那就動手實現(xiàn)一下吧,希望對大家學(xué)習(xí)javascript程序設(shè)計有所幫助。
相關(guān)文章
微信企業(yè)號開發(fā)之微信考勤Cookies的使用
使用微信考勤,每次使用微信企業(yè)號開發(fā):微信用戶信息和web網(wǎng)頁的session的關(guān)系這個里邊的方法,調(diào)用微信的接口,有點慢,微信官方也推薦使用Cookies,但如何使用Cookies,自己卻一直沒有搞清楚。下面小編幫大家解決難題,需要的朋友可以參考下2015-09-09Bootstrap布局之柵格系統(tǒng)學(xué)習(xí)筆記
這篇文章主要為大家詳細介紹了Bootstrap布局之柵格系統(tǒng)的學(xué)習(xí)筆記,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05JavaScript進制數(shù)之間的互相轉(zhuǎn)換
這篇文章主要介紹了JavaScript進制數(shù)之間的互相轉(zhuǎn)換,進制轉(zhuǎn)換是人們利用符號來計數(shù)的方法,下文基于JavaScript實現(xiàn)進制數(shù)之間的轉(zhuǎn)換,有一定的知識性參考價值,需要的小伙伴可以參考一下2022-05-05