一、代码
<div class="layui-col-xs3" style="text-align: right;"> <button type="button" id="send-sms-btn" class="layui-btn" onclick="sendSms()">发送</button> <button type="button" id="send-sms-time" style="display: none" class="layui-btn">60</button> </div>
var countDownT; function sendSms() { var mobile = $("#LAY-user-login-username").val(); if (mobile.length == 0){ layer.msg('请输入手机号',{icon: 5}); return; } $.get("{:url('sms')}?mobile="+mobile, function (res) { if (res.code == 0){ countDownT = 60; $("#send-sms-time").html(countDownT); $("#send-sms-btn").hide(); $("#send-sms-time").show(); setTime(); } else{ layer.msg(res.msg,{icon: 5}); } }, 'json'); } function setTime() { if (countDownT == 0){ $("#send-sms-btn").text("发送"); $("#send-sms-btn").show(); $("#send-sms-time").hide(); } else{ $("#send-sms-time").text(countDownT+' s'); countDownT--; setTimeout(function () { setTime(); },1000) } }