原文地址:http://www.oschina.net/code/snippet_2001568_48311
特別說(shuō)明:cookie最初創(chuàng)建的有效時(shí)間是60秒。也就是說(shuō)景用,你如果在倒計(jì)時(shí)為20的時(shí)候坦胶,關(guān)閉網(wǎng)頁(yè).20秒之后再打開(kāi)叔扼,是不會(huì)有倒計(jì)時(shí)顯示的街氢;但是突诬,如果倒計(jì)時(shí)為20時(shí)苫拍,關(guān)閉頁(yè)面,如果在20秒內(nèi)重新打開(kāi)頁(yè)面旺隙,是有倒計(jì)時(shí)顯示的绒极。
CSS:
.inp_number{ width: 200px; height: 30px;}
.sendBtn{ width: 120px; height: 35px; background: #4b8ccb; border: none; color: #FFF; cursor: pointer;}
.gray{ background: #c5c5c5; }
HTML:
<div class="message_send">
<input type="text" class="inp_number"/>
<input type="button" value="發(fā)送驗(yàn)證碼" class="sendBtn" id="send"/>
</div>
JS:
$(function(){
$('#send').click({function(){
sendCode($("#send"));
});
v = getCookieValue("secondsremained") ? getCookieValue("secondsremained") : 0; //獲取cookie值
if(v>0){
settime($("#send"));//開(kāi)始倒計(jì)時(shí)
}
}
//發(fā)送驗(yàn)證碼
function sendCode(obj){
var phoneNum = $('.inp_number').val();
var resule = isPhoneNum();
if(result){
// doPostBack(url,backFunc1,{"phoneNum":phonenum});
addCookie("secondsremained",60,60); //添加cookie記錄,有效時(shí)間為60s
settime(obj); //開(kāi)始倒計(jì)時(shí)
}
}
//將手機(jī)利用ajax提交到后臺(tái)的發(fā)短信接口
function doPostBack(url,backFunc,queryParam){
$.ajax({
async : false,
cache : false,
type : 'POST',
url : url, //請(qǐng)求的action路徑
data : queryParam,
error : function(){
//請(qǐng)求失敗處理函數(shù)
}
success : backFunc
});
}
function backFunc1(data){
var d = $.parseJSON(data);
if(!d.success){
alert(d.msg);
}else{
//返回驗(yàn)證碼
alert("模擬驗(yàn)證碼:" + d.msg);
$("#code").val(d.msg);
}
}
//發(fā)送驗(yàn)證碼是添加cookie
function addCookie(name,value,expirseHours){
var cookieString = name + "=" + escape(value);
//判斷是否設(shè)置過(guò)期時(shí)間蔬捷,0代表關(guān)閉瀏覽器時(shí)失效
if(expiresHours > 0){
var date = new Date();
date.setTime(date.getTime() + expiresHours * 1000);
cookieString = cookieString + ";expires=" + date.toUTCString();
}
document.cookie = cookieString;
}
//修改cookie的值
function editCookie(name,value,expiresHours){
var cookieString = name + "=" + escape(value);
if(expiresHours > 0){
var date = new Date();
date.setTime(date.getTime() + expiresHours * 1000);
cookieString = cookieString + ";expires=" + date.toUTCString();
}
document.cookie = cookieString;
}
//根據(jù)名字獲取cookie的值
function getCookieValue(name){
var strCookie = document.cookie;
var arrCookie = strCookie.split("; ");
for(var i=0; i < arrCookie.length; i++){
var arr = arrCookie[i].split("=");
if(arr[0] == name){
return unescape(arr[1]);
break;
}
}
}
//倒計(jì)時(shí)
var countdown;
function settime(obj){
countdown = getCookieValue("secondsremained");
if(countdown == 0){
obj.removeAttr("disabled");
obj.val("免費(fèi)獲取驗(yàn)證碼");
return;
}else{
obj.attr("disabled" , true);
obj.val("重新發(fā)送(" + countdown + ")" );
countdown--;
editCookie("secondsremained", countdown, countdown+1);
}
setTimeout(function(){
settime(obj)
},1000); //每1000秒執(zhí)行一次
}
//驗(yàn)證手機(jī)號(hào)碼是否合法
function isPhoneNum(){
var phoneNum = $('.inp_number').val();
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
if(phoneNum == '' || !myreg.test(phoneNum)){
alert('請(qǐng)輸入正確的手機(jī)號(hào)碼垄提!');
return false;
}else{
return false;
}
}