微信小程序+WEB使用JS實現註冊【60s】倒計時功能開發步驟:javascript 一、效果圖:html
二、頁面僅僅利用了JS的相關功能,包含:wxml、js、wxssjava 2.1wxml頁面代碼:jquery <text>綁定手機</text> <form bindsubmit="bindMobile"> <view class="form_group"> <text>手 機:</text> <input type="number" placeholder="請輸入手機號" maxlength="11" name="data_phone" value="" auto-focus="true" bindblur="blur_mobile" /> <button type="button" class="{{is_show?'show':'hide'}}" bindtap="clickVerify">獲取驗證碼</button> <button type="button" class="{{is_show?'hide':'show'}}">從新發送{{last_time}}秒</button> </view> <input type="number" placeholder="請輸入驗證碼" maxlength="6" name="data_verify" value=""/> <button class="save_btn" form-type="submit">確認綁定</button> </form> 2.2 js頁面代碼:web var countdown = 60; var settime = function (that) { if (countdown == 0) { that.setData({ is_show: true }) countdown = 60; return; } else { that.setData({ is_show:false, last_time:countdown }) countdown--; } setTimeout(function () { settime(that) } , 1000) } Page({ /** * 頁面的初始數據 */ data: { last_time:'', is_show:true }, clickVerify:function(){ var that = this; // 將獲取驗證碼按鈕隱藏60s,60s後再次顯示 that.setData({ is_show: (!that.data.is_show) //false }) settime(that); } }) 2.3 wxss頁面代碼:小程序 /* 發送驗證碼按鈕隱藏,並展現倒數60s提示 */ .hide{ display: none; } .show{ display: block; }
三、上面講的是微信小程序的,那麼咱們通常web端或者移動端的應該是什麼樣呢?微信小程序 其實,方法都差很少,這裏也貼出來僅供你們參考微信 <!-- 這段代碼(html)是從腳本之家挪過來的,信譽度應該是很高的,你們能夠放心使用 --> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> var countdown=60; function settime(obj) { if (countdown == 0) { obj.removeAttribute("disabled"); obj.value="免費獲取驗證碼"; countdown = 60; return; } else { obj.setAttribute("disabled", true); obj.value="從新發送(" + countdown + ")"; countdown--; } setTimeout(function() { settime(obj) } ,1000) } </script> <body> <input type="button" id="btn" value="免費獲取驗證碼" onclick="settime(this)" /> </body> </html> |