js 工具类方法

来源:互联网 发布:软件测试职位有哪些 编辑:程序博客网 时间:2024/06/02 04:47
 //几秒后可以发送短信 timer 时间 ,btnId发送短信ID,tipsId正在发送ID clickName事件名称
function timer(timer,btnId,tipsId,clickName){
timer = getCookieValue("secondsremained") ? getCookieValue("secondsremained"):timer;
//显示短信发送框
$("#"+btnId+"").hide();
//隐藏正在发送框
$("#"+tipsId+"").show();
$("#"+tipsId+"").text((timer<=0)?"发送短信验证码":(""+(timer)+"秒后可以再发送短信验证码"));
var senderTime = setInterval(function(){
if(timer <= 0 ){
clearInterval(senderTime);
//显示短信发送框
$("#"+btnId+"").show();
$("#"+tipsId+"").hide();
$("#"+btnId+"").text("发送短信验证码");
$("#"+btnId+"_error").text("");
//恢复发送短信点击事件
$("#"+btnId+"").attr("onclick",""+clickName+"(this)");
return false;
}else{
$("#"+tipsId+"").text(""+(timer--)+"秒后可以再发送短信验证码");
editCookie("secondsremained",timer,timer+1);
}
},1000);
};


function tellTimer(timer,btnId,tipsId,clickName){
timer = getCookieValue("tellPhone") ? getCookieValue("tellPhone"):timer;
//显示短信发送框
$("#"+btnId+"").hide();
//隐藏正在发送框
$("#"+tipsId+"").show();
$("#"+tipsId+"").text((timer<=0)?"发送短信验证码":(""+(timer)+"秒后可以发送短信验证码"));
var senderTime = setInterval(function(){
if(timer <= 0 ){
clearInterval(senderTime);
//显示短信发送框
$("#"+btnId+"").show();
$("#"+tipsId+"").hide();
$("#"+btnId+"").text("发送短信验证码");
$("#"+btnId+"_error").text("");
//恢复发送短信点击事件
$("#"+btnId+"").attr("onclick",""+clickName+"(this)");
return false;
}else{
$("#"+tipsId+"").text(""+(timer--)+"秒后可以发送短信验证码");
editCookie("tellPhone",timer,timer+1);
}
},1000);
};


//发送手机验证码时添加cookie
function addCookie(name,value,expiresHours){ 
    var cookieString=name+"="+escape(value); 
    //判断是否设置过期时间,0代表关闭浏览器时失效
    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.toGMTString(); 
    } 
      document.cookie=cookieString; 
}; 


//根据名字获取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;
        } 
      } 
 };
 
/*空判断*/
function isEmpty(val) {
val = $.trim(val);
if (val == null)
return true;
if (val == undefined || val == 'undefined')
return true;
if (val == "")
return true;
if (val.length == 0)
return true;
if (!/[^(^\s*)|(\s*$)]/.test(val))
return true;
return false;
};


/*非空判断*/
function isNotEmpty(val) {
return !isEmpty(val);
};
0 0
原创粉丝点击