Home > Web Front-end > JS Tutorial > body text

js code to implement a 60-second countdown when clicking the button_javascript skills

WBOY
Release: 2016-05-16 15:24:29
Original
2136 people have browsed it

For example, the SMS authentication function on some websites has an effect similar to that after clicking the button, it will count down for 60 seconds before clicking send again.

This example uses Javascript to implement the function of counting down for 60 seconds before clicking the button again to send the verification code after clicking the button.

Example 1: Javascript implements the effect of clicking the button and counting down for 60 seconds before clicking again to send

<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="重新发送(" + wait + ")";
   wait--;
   setTimeout(function() {
    time(o)
   },
   1000)
  }
 }
document.getElementById("btn").onclick=function(){time(this);}
</script>
Copy after login

Example 2: Click the button to display the 60-second countdown js code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>无标题文档</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="重新发送(" + countdown + ")"; 
countdown--; 
} 
setTimeout(function() { 
settime(val) 
},1000) 
} 
</script> 
</body> 
</html>
Copy after login

Example 3: After clicking the button, there will be a 60-second countdown before you can continue to click. The countdown can also be displayed on the button

Expected rendering:

This is the code on the WeChat public platform

function E() {
var e = $("#mobile"), t = (new Date).getTime(), n = Math.floor((t - b) / 1e3);
g && clearTimeout(g), n >= 60 &#63; (e.prop("readonly", !1), y = !0, $("#sendmobile").html("发送验证码").attr("disabled", !1).removeClass("btn_disabled")) : (e.prop("readonly", !0), y = !1, $("#sendmobile").attr("disabled", !0).addClass("btn_disabled").html("%s秒后可重发".sprintf(60 - n)), g = setTimeout(E, 1e3));
}
function S() {
function e() {
if (!y) return;
var e = $.trim(n.val());
l.mobile(e) &#63; 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 &#63; "/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("验证码已经发送"); else {
switch (t) {
case -13:
u.err("登录超时,请重新登录");
break;
case -35:
u.err("该手机已经登记过2次,请使用别的手机号进行用户信息登记");
break;
default:
u.err("验证码发送失败");
}
b = 0;
}
}));
});
}
Copy after login

The editor just didn’t know how to call this code into his own code. After some guidance from experts, he came up with some clues.

Expert’s idea of ​​solving the problem: If it is strict, this should be combined with the background acquisition time, otherwise others can just refresh it.
If it is not strict, you can also use a 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("点击发送")
    $.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("点击发送")
    $.cookie("dis2","")
    return
   }
  }

  $obj.text(time+"秒后重新发送")

 }

 $(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);
 //没有被禁用时禁用并执行倒计时
 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" >点击发送</button><br>
<button id="b2" class="cd" >点击发送</button><br>
Copy after login

If you have any ideas through these three examples, let’s implement it. I hope it will be helpful to everyone in learning javascript programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template