This article mainly introduces the sample code for js judgment of 60 seconds and countdown. If you need a friend, you can refer to it and hope to help you determine the 60 seconds to the timer using js, first, define a variable refreshTime. When an event is triggered, check whether the previous request was successful for more than 60 seconds. If not, a prompt is displayed,
Otherwise, the request is allowed to continue. After the request is successful, the current time is assigned to refreshTime. The specific implementation method is as follows;
The Code is as follows:
UsingNamespace ("Biz. AccountCenter") ["CellPhoneValidation"] = {
RefreshTime: 0, // variable
CheckRefreshGet: function (timeLimit) // check time. timelimit is the interval (in seconds) required for incoming requests. For example: 60
{
Var nowTime = new Date ();
Var nowMinitePoint = nowTime. getHours () * 3600 + nowTime. getMinutes () * 60 + nowTime. getSeconds ();
If (nowMinitePoint-Biz.AccountCenter.CellPhoneValidation.refreshTime {
Return false;
}
Return true;
},
ResetRefreshGet: function () // after the request is successful, call the method to reset the defined variable to the current time.
{
Var nowTime = new Date ();
Var nowMinitePoint = nowTime. getHours () * 3600 + nowTime. getMinutes () * 60 + nowTime. getSeconds ();
Biz. AccountCenter. CellPhoneValidation. refreshTime = nowMinitePoint;
},
DynamicMessage: function (timeSecond) // countdown method timeSecond is from how many seconds, for example: 60
{
Var showTimmer;
If (showTimmer ){
ClearTimeout (showTimmer );
}
If (timeSecond = dynamicValidate. refreshTimeLimit)
{
Var messageRefresh = $. newegg. format (dynamicValidate. refreshSpanMessage, timeSecond );
$ ("# SpanRefresh" pai.html ("" + messageRefresh + "");
$ ("# SpanRefresh"). attr ("class", "button btn_yanz_disable ");
TimeSecond --;
}
ShowTimmer = setTimeout (function (){
Var messageRefresh = $. newegg. format (dynamicValidate. refreshSpanMessage, timeSecond );
$ ("# SpanRefresh" pai.html ("" + messageRefresh + "");
TimeSecond --;
If (timeSecond <0 ){
ClearTimeout (showTimmer );
$ ("# SpanRefresh"). attr ("class", "button btn_yanz ");
$ ("# SpanRefresh" pai.html ("" + dynamicValidate. refreshMessage + "");
} Else {
Biz. AccountCenter. CellPhoneValidation. dynamicMessage (timeSecond );
$ ("# SpanRefresh"). attr ("class", "button btn_yanz_disable ");
}
},1000 );
},
Create: function (obj, page, isCancelPhone) // method of each request call
{
If (! Biz. AccountCenter. CellPhoneValidation. checkRefreshGet (dynamicValidate. refreshTimeLimit) // js checks the 60 s interval for each request
{
$ ("# Valiateerror" ).empty().html ("" + $. newegg. format ($ Resource. BuildContent ("updated"), 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
});
}
}