基于jQuery實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼后的倒計(jì)時(shí)功能(無視頁面關(guān)閉)
相關(guān)閱讀:
基于JS實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼后的倒計(jì)時(shí)功能(無視頁面刷新,頁面關(guān)閉不進(jìn)行倒計(jì)時(shí)功能)
今天測試提了一個(gè)bug,發(fā)送短信倒計(jì)時(shí)功能,要求關(guān)閉頁面也要進(jìn)行倒計(jì)時(shí)。這想到了,當(dāng)年我參與的周杰倫演唱會(huì)的先付先搶功能。與之類似,只不過,那個(gè)項(xiàng)目的時(shí)間都是服務(wù)器時(shí)間,本人目前有點(diǎn)偷懶,就用客戶端的時(shí)間了。
一下是完整的代碼,只不過在客戶端的效率不是很好。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.js"></script>
<script src="http://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.js"></script>
<!--<script src="jquery.min.js"></script>-->
<!-- <script src="jquery.cookie.js"></script>-->
<!-- <script src="http://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>-->
</head>
<body>
<input id="phonenum" type="text" value="18518181818"/>
<input id="second" type="button" value="免費(fèi)獲取驗(yàn)證碼" />
</body>
<script>
$(function(){
$("#second").click(function (){
sendCode($("#second"));
});
checkCountdown();
})
//校驗(yàn)打開頁面的時(shí)候是否要繼續(xù)倒計(jì)時(shí)
function checkCountdown(){
var secondsremained = $.cookie("secondsremained");
if(secondsremained){
var date = new Date(unescape(secondsremained));
setCoutDown(date,$("#second"));
}
}
//發(fā)送驗(yàn)證碼
function sendCode(obj){
var phonenum = $("#phonenum").val();
var result = isPhoneNum();
if(result){
//加載ajax 獲取驗(yàn)證碼的方法
// doPostBack('${base}/login/getCode.htm',backFunc1,{"phonenum":phonenum});
var date = new Date();
addCookie("secondsremained",date.toGMTString(),60);//添加cookie記錄,有效時(shí)間60s
setCoutDown(date,obj);
}
}
var nowDate = null;
var time_difference = 0;
var count_down = 0;
function setCoutDown(date,obj) {
nowDate = new Date();
time_difference = ((nowDate- date)/1000).toFixed(0);
count_down = 60 - time_difference;
console.log(count_down);
if(count_down<=0){
obj.removeAttr("disabled");
obj.val("免費(fèi)獲取驗(yàn)證碼");
addCookie("secondsremained","",60);//添加cookie記錄,有效時(shí)間60s
return;
}
obj.attr("disabled", true);
obj.val("重新發(fā)送(" + count_down + ")");
setTimeout(function() { setCoutDown(date,obj) },1000) //每1000毫秒執(zhí)行一次
}
//發(fā)送驗(yàn)證碼時(shí)添加cookie
function addCookie(name,value,expiresHours){
//判斷是否設(shè)置過期時(shí)間,0代表關(guān)閉瀏覽器時(shí)失效
if(expiresHours>0){
var date=new Date();
date.setTime(date.getTime()+expiresHours*1000);
$.cookie(name, escape(value), {expires: date});
}else{
$.cookie(name, escape(value));
}
}
//校驗(yàn)手機(jī)號(hào)是否合法
function isPhoneNum(){
var phonenum = $("#phonenum").val();
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
if(!myreg.test(phonenum)){
alert('請(qǐng)輸入有效的手機(jī)號(hào)碼!');
return false;
}else{
return true;
}
}
</script>
</html>
以上所述是小編給大家介紹的基于jQuery實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼后的倒計(jì)時(shí)功能(無視頁面關(guān)閉),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
jquery實(shí)現(xiàn)LED廣告牌旋轉(zhuǎn)系統(tǒng)圖片切換效果代碼分享
這篇文章主要介紹了jquery實(shí)現(xiàn)LED廣告牌旋轉(zhuǎn)系統(tǒng)圖片切換效果類似路邊場景,很實(shí)用的代碼,推薦給大家,有需要的小伙伴可以參考下。2015-08-08
Validform驗(yàn)證時(shí)可以為空否則按照指定格式驗(yàn)證
這篇文章主要介紹了Validform驗(yàn)證時(shí)可以為空否則按照指定格式驗(yàn)證的相關(guān)資料,需要的朋友可以參考下2017-10-10
jQuery中filter()和find()的區(qū)別深入了解
一直不是很清楚filter()方法和find()方法的區(qū)別,看jQuery Cookbook一書后,終于算是搞清楚了,下面將新的與大家分享下2013-09-09
jQuery簡單實(shí)現(xiàn)的HTML頁面文本框模糊匹配查詢功能完整示例
這篇文章主要介紹了jQuery簡單實(shí)現(xiàn)的HTML頁面文本框模糊匹配查詢功能,涉及jQuery事件響應(yīng)模擬列表框的下拉數(shù)據(jù)展示與隱藏,以及元素遍歷、匹配等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
jQuery插件dataTables添加序號(hào)列的方法
這篇文章主要介紹了jQuery插件dataTables添加序號(hào)列的方法的相關(guān)資料,需要的朋友可以參考下2016-07-07
jQuery動(dòng)畫效果-slideUp slideDown上下滑動(dòng)示例代碼
jQuery動(dòng)畫上下滑動(dòng)效果在網(wǎng)頁中的應(yīng)用還是比較廣泛的,其實(shí)很簡單通過slideUp slideDown方法便可輕松實(shí)現(xiàn),具體如下,喜歡的朋友可以參考下,希望對(duì)大家學(xué)習(xí)jquery有所幫助2013-08-08
IE10中flexigrid無法顯示數(shù)據(jù)的解決方法
這篇文章主要介紹了IE10中flexigrid無法顯示數(shù)據(jù)的解決方法的相關(guān)資料,需要的朋友可以參考下2015-07-07

