Home > Web Front-end > JS Tutorial > js determine 60 seconds and countdown sample code_javascript skills

js determine 60 seconds and countdown sample code_javascript skills

WBOY
Release: 2016-05-16 17:02:45
Original
1066 people have browsed it

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;

Copy codeThe code is as follows:

usingNamespace("Biz.AccountCenter")["CellPhoneValidation"]={ 
    refreshTime:0, //变量

    checkRefreshGet:function(timeLimit) //检查时间,timelimit为传入请求需要间隔的时间(单位秒),比如: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() //请求成功之后,调用的方法,把定义的变量重置为当前时间
{
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
        });

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template