js 60秒倒计时函数,一个很简单的函数,用的时候都是自己写,发现每次都是调试好几次,甚至还有bug,为提高日后工作效率,就记录一下。
按钮:
<button id="vCodeButton" type="button" onclick="getVCode();">获取验证码</button>倒计时函数:
var wait = 60; function countdown(obj) { if (wait == 0) { obj.label = "获取验证码"; obj.setAttribute("onclick", "getVCode()"); wait = 60; } else { obj.label = "重新发送(" + wait + ")"; obj.setAttribute("onclick", ""); wait--; setTimeout(function () { countdown(obj); }, 1000) } }点击按钮逻辑:
var domain = 'localhost'; function getVCode(){ var phoneNo = document.getElementById("phoneNo").value; if (!phoneNo) { alert(null, "提示", "手机不能为空!"); return; } if (!REGEX_MOBILE_EXACT.test(phoneNo)) { alert(null, "提示", "手机不合法!"); return; } $.ajax({ url: "http://"+domain+"/mangocool/vcode", type: "POST", data: JSON.stringify({ "phoneNo": phoneNo, "action": "regist" }); dataType: "json", contentType: "application/json;charset=utf-8", beforeSend: function () { }, error: function (result) { Services.prompt.alert(null, "提示", "服务出错,请稍后重试!"); }, success: function (data) { if(data.code == 0){ // 倒计时函数 countdown(document.getElementById("vCodeButton")); } else { alert(null, "提示", data.msg); } }, complete: function (result) { } }); }
标签: js 倒计时 setTimeout
分享:
崇尚极简,热爱技术,喜欢唱歌,热衷旅行,爱好电子产品的一介码农。
联系QQ:58742094
联系电话:
工作邮箱:
当你的才华还撑不起你的野心的时候,你就应该静下心来学习,永不止步!
人生之旅历途甚长,所争决不在一年半月,万不可因此着急失望,招精神之萎葸。
Copyright 2015- 芒果酷(mangocool.com) All rights reserved. 湘ICP备14019394号
免责声明:本网站部分文章转载其他媒体,意在为公众提供免费服务。如有信息侵犯了您的权益,可与本网站联系,本网站将尽快予以撤除。