愚盦 2016-10-30
今天要写个获取验证码倒计时60秒,用到了setInterval。其中按钮点击后要禁用onclick事件(当然也可以在onclick事件中判断剩余时间小于60秒就returnfalse),当剩余时间为0秒时,则要恢复按钮的onclick事件,问题就出在恢复onclick上,enableObj函数一执行,就立即调用了func,导致了死循环,最后将$(obj).attr("onclick",func);改为$(obj).bind('click',func);问题解决
函数如下(其中闭包全局函数名yjzx,window.$yjzxjs=yjzx)
enableObj:function(obj,func){
if(func){
$(obj).attr("onclick",func);
}
},
//倒计时60秒
getValidCode:function(){
tempTimer=setInterval('$yjzxjs.getValidCode_()',1000);
},
getValidCode_:function(){
if(countSeconds<=0){
if(tempTimer){
clearInterval(tempTimer);
}
yjzx.setHtml('validcodeButton','重新获取');
yjzx.enableObj($api.byId('validcodeButton'),yjzx.getValidCode);
countSeconds=countSecondConst;
return;
}else{
yjzx.disableObj($api.byId('validcodeButton'));
yjzx.setHtml('validcodeButton',countSeconds+'秒后重新获取');
countSeconds--;
}
},
<table id="table" class="table table-striped table-bordered table-hover table-nowrap" width="100%&qu