事件:
//发送验证码 $('.js-sms-code').click(function(){ $(this).attr("disabled", "disabled").html("<span style='color:#666'><span id='countdown'>60</span>s 后再试</span>"); countdown(); var tel = $('#tel').val(); $.ajax({ url: "{sh::U('Home/sendSmscode')}", type:'POST', dataType:"json", data: {tel: tel}, success: function() { }, error: function() { $('.js-help-info').html("请求失败"); } }); })
点评:这里的countdown方法就是妙处。
看代码:
function countdown() { // 递归 setTimeout(function() { var time = $("#countdown").text(); if (time == 1) { $('.js-sms-code').removeAttr("disabled"); $('.js-sms-code').html("发送验证码"); } else { $("#countdown").text(time - 1); countdown(); } }, 1000); }
点评:如果time不等于1,就继续调用,同时时间减去一秒。setTimeout也很精髓。直至time减到1为止,移除disabled并更改内容为‘发送验证码'。
Copyright © 2019- howto1234.net 版权所有 湘ICP备2023021910号-3
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务