Home > Article > Web Front-end > js determine 60 seconds and countdown sample code_javascript skills
Use js to determine the time of 60 seconds. First define a variable refreshTime. When the event is triggered, check whether the time since the last successful request is now more than 60 seconds. If it is not more than 60 seconds, a prompt will pop up.
Otherwise, the request is allowed to continue. After the request is successful, the current time is assigned to refreshTime. The specific implementation method;
if(nowMinitePoint-Biz.AccountCenter.CellPhoneValidation.refreshTime
return false;
}
return true;
},
resetRefreshGet:function() //请求成功之后,调用的方法,把定义的变量重置为当前时间
{
var nowTime = new Date();
var nowMinitePoint=nowTime.getHours()*3600+nowTime.getMinutes()*60+nowTime.getSeconds();
Biz.AccountCenter.CellPhoneValidation.refreshTime=nowMinitePoint;
},
dynamicMessage:function(timeSecond) //倒计时方法 timeSecond为从多少秒开始,比如:60
{
var showTimmer;
if (showTimmer) {
clearTimeout(showTimmer);
}
if(timeSecond==dynamicValidate.refreshTimeLimit)
{
var messageRefresh =$.newegg.format(dynamicValidate.refreshSpanMessage,timeSecond);
$("#spanRefresh").html(""+messageRefresh+"");
$("#spanRefresh").attr("class","button btn_yanz_disable");
timeSecond--;
}
showTimmer = setTimeout(function () {
var messageRefresh =$.newegg.format(dynamicValidate.refreshSpanMessage,timeSecond);
$("#spanRefresh").html(""+messageRefresh+"");
timeSecond--;
if (timeSecond < 0) {
clearTimeout(showTimmer);
$("#spanRefresh").attr("class","button btn_yanz");
$("#spanRefresh").html(""+dynamicValidate.refreshMessage+"");
} else {
Biz.AccountCenter.CellPhoneValidation.dynamicMessage(timeSecond);
$("#spanRefresh").attr("class","button btn_yanz_disable");
}
}, 1000);
},
create: function(obj,page,isCancelPhone) //每次请求调用的方法
{
if(!Biz.AccountCenter.CellPhoneValidation.checkRefreshGet(dynamicValidate.refreshTimeLimit)) //每次请求的时候,js检查60s时间间隔
{
$("#valiateerror").empty().html(""+$.newegg.format($Resource.BuildContent("AccountCenter_ModifyDyanmic_CanNotRepeatClick"),dynamicValidate.refreshTimeLimit)+"").show();
$("#mobilewarning").hide();
return;
}
$.get("url",data,function(){
//If successful
Biz.AccountCenter.CellPhoneValidation.dynamicMessage(dynamicValidate.refreshTimeLimit);//Countdown
Biz .AccountCenter.CellPhoneValidation.resetRefreshGet();//Reset time
});