js代碼實(shí)現(xiàn)點(diǎn)擊按鈕出現(xiàn)60秒倒計(jì)時(shí)
比如,一些網(wǎng)站上的手機(jī)短信認(rèn)證的功能,有類似實(shí)現(xiàn)點(diǎn)擊按鈕后,倒計(jì)時(shí)60秒才能再次點(diǎn)擊發(fā)送的效果。
此例子用Javascript實(shí)現(xiàn)點(diǎn)擊按鈕后,倒計(jì)時(shí)60秒才能再次點(diǎn)擊發(fā)送驗(yàn)證碼的功能。
例子1:Javascript 實(shí)現(xiàn) 點(diǎn)擊按鈕 倒計(jì)時(shí)60秒方可再次點(diǎn)擊發(fā)送的效果
<input type="button" id="btn" value="免費(fèi)獲取驗(yàn)證碼" />
<script type="text/javascript">
var wait=60;
function time(o) {
if (wait == 0) {
o.removeAttribute("disabled");
o.value="免費(fèi)獲取驗(yàn)證碼";
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:點(diǎn)擊按鈕出現(xiàn)60秒倒計(jì)時(shí)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="免費(fèi)獲取驗(yàn)證碼" onclick="settime(this)" />
<script type="text/javascript">
var countdown=60;
function settime(val) {
if (countdown == 0) {
val.removeAttribute("disabled");
val.value="免費(fèi)獲取驗(yàn)證碼";
countdown = 60;
} else {
val.setAttribute("disabled", true);
val.value="重新發(fā)送(" + countdown + ")";
countdown--;
}
setTimeout(function() {
settime(val)
},1000)
}
</script>
</body>
</html>
例子3:點(diǎn)擊按鈕后,60秒倒計(jì)時(shí)后才能繼續(xù)可以點(diǎn)擊,按鈕上還能顯示倒計(jì)時(shí)
預(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ā)送驗(yàn)證碼").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("驗(yàn)證碼已經(jīng)發(fā)送"); else {
switch (t) {
case -13:
u.err("登錄超時(shí),請重新登錄");
break;
case -35:
u.err("該手機(jī)已經(jīng)登記過2次,請使用別的手機(jī)號進(jìn)行用戶信息登記");
break;
default:
u.err("驗(yàn)證碼發(fā)送失敗");
}
b = 0;
}
}));
});
}
這段代碼小編就是不知道怎么才能調(diào)用到自己的代碼上來,經(jīng)過高人指點(diǎn)有了一些頭尋。
高人的解題思路:如果嚴(yán)格的話,這個(gè)還要結(jié)合后臺獲取時(shí)間的,要不然別人刷新一下就行了。
如果不嚴(yán)格,用個(gè)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("點(diǎn)擊發(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("點(diǎn)擊發(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);
//沒有被禁用時(shí)禁用并執(zhí)行倒計(jì)時(shí)
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" >點(diǎn)擊發(fā)送</button><br>
<button id="b2" class="cd" >點(diǎn)擊發(fā)送</button><br>
更多關(guān)于倒計(jì)時(shí)的文章請查看專題: 《倒計(jì)時(shí)功能》
大家通過這三個(gè)例子講解有沒有點(diǎn)思路了,那就動手實(shí)現(xiàn)一下吧,希望對大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
- JS 倒計(jì)時(shí)實(shí)現(xiàn)代碼(時(shí)、分,秒)
- JS實(shí)現(xiàn)倒計(jì)時(shí)(天數(shù)、時(shí)、分、秒)
- 簡單易用的倒計(jì)時(shí)js代碼
- 2種簡單的js倒計(jì)時(shí)方式
- 原生JS實(shí)現(xiàn)簡單的倒計(jì)時(shí)功能示例
- js幾秒以后倒計(jì)時(shí)跳轉(zhuǎn)示例
- 最全面的JS倒計(jì)時(shí)代碼
- JS實(shí)現(xiàn)十分鐘倒計(jì)時(shí)代碼實(shí)例
- Javascript倒計(jì)時(shí)代碼
- JavaScript實(shí)現(xiàn)倒計(jì)時(shí)功能2種方法實(shí)例
相關(guān)文章
ExpressJS使用express-ws的實(shí)例詳解
這篇文章主要介紹了ExpressJS使用express-ws的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
純 JS 實(shí)現(xiàn)放大縮小拖拽功能(完整代碼)
這篇文章主要介紹了純js實(shí)現(xiàn)放大縮小拖拽功能,文中給大家提到了在開發(fā)過程中遇到的一些問題及解決方法,需要的朋友可以參考下2019-11-11
動態(tài)添加刪除表格行的js實(shí)現(xiàn)代碼
本篇文章主要是對動態(tài)添加刪除表格行的js實(shí)現(xiàn)代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
微信企業(yè)號開發(fā)之微信考勤Cookies的使用
使用微信考勤,每次使用微信企業(yè)號開發(fā):微信用戶信息和web網(wǎng)頁的session的關(guān)系這個(gè)里邊的方法,調(diào)用微信的接口,有點(diǎn)慢,微信官方也推薦使用Cookies,但如何使用Cookies,自己卻一直沒有搞清楚。下面小編幫大家解決難題,需要的朋友可以參考下2015-09-09
Bootstrap布局之柵格系統(tǒng)學(xué)習(xí)筆記
這篇文章主要為大家詳細(xì)介紹了Bootstrap布局之柵格系統(tǒng)的學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
JavaScript進(jìn)制數(shù)之間的互相轉(zhuǎn)換
這篇文章主要介紹了JavaScript進(jìn)制數(shù)之間的互相轉(zhuǎn)換,進(jìn)制轉(zhuǎn)換是人們利用符號來計(jì)數(shù)的方法,下文基于JavaScript實(shí)現(xiàn)進(jìn)制數(shù)之間的轉(zhuǎn)換,有一定的知識性參考價(jià)值,需要的小伙伴可以參考一下2022-05-05

