<p>比如,一些网站上的手机短信认证的功能,有类似实现点击按钮后,倒计时60秒才能再次点击发送的效果。</p><p>此例子用Javascript实现点击按钮后,倒计时60秒才能再次点击发送验证码的功能。</p><p>例子1:Javascript 实现 点击按钮 倒计时60秒方可再次点击发送的效果</p><pre class="brush:html;toolbar:false">&lt;inputtype=&quot;button&quot;id=&quot;btn&quot;value=&quot;免费获取验证码&quot;/&gt; &lt;scripttype=&quot;text/javascript&quot;&gt; varwait=60; functiontime(o){ if(wait==0){ o.removeAttribute(&quot;disabled&quot;); o.value=&quot;免费获取验证码&quot;; wait=60; }else{ o.setAttribute(&quot;disabled&quot;,true); o.value=&quot;重新发送(&quot;+wait+&quot;)&quot;; wait--; setTimeout(function(){ time(o) }, 1000) } } document.getElementById(&quot;btn&quot;).onclick=function(){time(this);} &lt;/script&gt;</pre><p>例子2:点击按钮出现60秒倒计时js代码</p><pre class="brush:html;toolbar:false">&lt;!DOCTYPEhtmlPUBLIC&quot;-//W3C//DTDXHTML1.0Transitional//EN&quot;&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt; &lt;htmlxmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt; &lt;head&gt; &lt;metahttp-equiv=&quot;Content-Type&quot;content=&quot;text/html;charset=utf-8&quot;/&gt; &lt;title&gt;无标题文档&lt;/title&gt; &lt;scripttype=&quot;text/javascript&quot;src=&quot;js/jquery.js&quot;&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;inputtype=&quot;button&quot;id=&quot;btn&quot;value=&quot;免费获取验证码&quot;onclick=&quot;settime(this)&quot;/&gt; &lt;scripttype=&quot;text/javascript&quot;&gt; varcountdown=60; functionsettime(val){ if(countdown==0){ val.removeAttribute(&quot;disabled&quot;); val.value=&quot;免费获取验证码&quot;; countdown=60; }else{ val.setAttribute(&quot;disabled&quot;,true); val.value=&quot;重新发送(&quot;+countdown+&quot;)&quot;; countdown--; } setTimeout(function(){ settime(val) },1000) } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</pre><p>例子3:点击按钮后,60秒倒计时后才能继续可以点击,按钮上还能显示倒计时</p><p>预想的效果图:</p><p>这是微信公众平台上的代码</p><pre class="brush:js;toolbar:false">functionE(){ vare=$(&quot;#mobile&quot;),t=(newDate).getTime(),n=Math.floor((t-b)/1e3); g&amp;&amp;clearTimeout(g),n&gt;=60?(e.prop(&quot;readonly&quot;,!1),y=!0,$(&quot;#sendmobile&quot;).html(&quot;发送验证码&quot;).attr(&quot;disabled&quot;,!1).removeClass(&quot;btn_disabled&quot;)):(e.prop(&quot;readonly&quot;,!0),y=!1,$(&quot;#sendmobile&quot;).attr(&quot;disabled&quot;,!0).addClass(&quot;btn_disabled&quot;).html(&quot;%s秒后可重发&quot;.sprintf(60-n)),g=setTimeout(E,1e3)); } functionS(){ functione(){ if(!y)return; vare=$.trim(n.val()); l.mobile(e)?t.attr(&quot;disabled&quot;,!1).removeClass(&quot;btn_disabled&quot;):t.attr(&quot;disabled&quot;,!0).addClass(&quot;btn_disabled&quot;); } vart=$(&quot;#sendmobile&quot;),n=$(&quot;#mobile&quot;); n.keyup(e).blur(e),e(),t.click(function(){ vare; t.attr(&quot;disabled&quot;)!==&quot;disabled&quot;&amp;&amp;(e=&quot;+86&quot;+$.trim(n.val()),b=(newDate).getTime(),E(),o.post({ url:w?&quot;/cgi-bin/formbyskey&quot;:&quot;/acct/formbyticket&quot;, data:{ form:&quot;mobile&quot;, action:&quot;set&quot;, f:&quot;json&quot;, mobile:e }, mask:!1 },function(e){ vart=e.BaseResp.Ret; if(t==0)u.suc(&quot;验证码已经发送&quot;);else{ switch(t){ case-13: u.err(&quot;登录超时,请重新登录&quot;); break; case-35: u.err(&quot;该手机已经登记过2次,请使用别的手机号进行用户信息登记&quot;); break; default: u.err(&quot;验证码发送失败&quot;); } b=0; } })); }); }</pre><p>这段代码小编就是不知道怎么才能调用到自己的代码上来,经过高人指点有了一些头寻。</p><p>高人的解题思路:如果严格的话,这个还要结合后台获取时间的,要不然别人刷新一下就行了。</p><p> 如果不严格,用个cookie也可以。</p><pre class="brush:js;toolbar:false">&lt;scriptsrc=&quot;http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js&quot;&gt;&lt;/script&gt; &lt;scriptsrc=&quot;http://yukon12345.com/yukon12345.com/js/jquery.cookie.js&quot;&gt;&lt;/script&gt; &lt;script&gt; time1=$.cookie(&quot;time1&quot;)||60; time2=$.cookie(&quot;time2&quot;)||60; dis1=$.cookie(&quot;dis1&quot;) dis2=$.cookie(&quot;dis2&quot;) functioncountDown($obj){ vartime; if($obj.attr(&quot;id&quot;)==&quot;b1&quot;) { time=--time1; $.cookie(&quot;time1&quot;,time,{&quot;expires&quot;:1}); if(time&lt;=0){ time1=60; $obj[0].disabled=!$obj[0].disabled clearInterval(inter1) $obj.text(&quot;点击发送&quot;) $.cookie(&quot;dis1&quot;,&quot;&quot;) return } } if($obj.attr(&quot;id&quot;)==&quot;b2&quot;) {time=--time2; $.cookie(&quot;time2&quot;,time,{&quot;expires&quot;:1}); if(time&lt;=0){ time1=60; $obj[0].disabled=!$obj[0].disabled clearInterval(inter2) $obj.text(&quot;点击发送&quot;) $.cookie(&quot;dis2&quot;,&quot;&quot;) return } } $obj.text(time+&quot;秒后重新发送&quot;) } $(function(){ if(dis1=&quot;dis&quot;){ $(&quot;#b1&quot;)[0].disabled=&#39;disabled&#39; inter1=setInterval(function(){countDown($(&quot;#b1&quot;))},1000) } if(dis2=&quot;dis&quot;){ $(&quot;#b2&quot;)[0].disabled=&#39;disabled&#39; inter2=setInterval(function(){countDown($(&quot;#b2&quot;))},1000) } $(&quot;.cd&quot;).bind(&quot;click&quot;,function(){ $this=$(this); //没有被禁用时禁用并执行倒计时 if(!$this[0].disabled){ $this[0].disabled=&#39;disabled&#39;; if($this.attr(&quot;id&quot;)==&quot;b1&quot;){ $.cookie(&quot;dis1&quot;,&quot;dis&quot;,{&quot;expires&quot;:1}) inter1=setInterval(function(){countDown($this)},1000) } if($this.attr(&quot;id&quot;)==&quot;b2&quot;){ $.cookie(&quot;dis2&quot;,&quot;dis&quot;,{&quot;expires&quot;:1}) inter2=setInterval(function(){countDown($this)},1000) } } }) }) &lt;/script&gt;</pre><p></p><p>&lt;button id=&quot;b1&quot; class=&quot;cd&quot; &gt;点击发送&lt;/button&gt;&lt;br&gt;</p><p>&lt;button id=&quot;b2&quot; class=&quot;cd&quot; &gt;点击发送&lt;/button&gt;&lt;br&gt;</p><p>大家通过这三个例子讲解有没有点思路了,那就动手实现一下吧,希望对大家学习javascript程序设计有所帮助。</p>
T:0.027212s,M:252.13 KB
返回顶部 留言