Js judge 60 seconds and countdown sample code

  • 2020-03-30 01:25:50
  • OfStack

Using js to determine 60 seconds to time, first define a variable called refreshTime. When the event is triggered, check to see if the last request was successful longer than 60 seconds. If not, the prompt will pop up.

Otherwise, the request is allowed to continue. After the request is successful, the current time is assigned to refreshTime.


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

    checkRefreshGet:function(timeLimit) //Check the time, timelimit is the time (in seconds) between 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<timeLimit )
     {
         return false;
     }
         return true;
    },
    resetRefreshGet:function() //After the request succeeds, the method is called 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) //The countdown method timeSecond starts with how many seconds, such as 60
    {
       var showTimmer;
       if (showTimmer) {
           clearTimeout(showTimmer);
       }

       if(timeSecond==dynamicValidate.refreshTimeLimit)
       {
            var messageRefresh =$.newegg.format(dynamicValidate.refreshSpanMessage,timeSecond);
           $("#spanRefresh").html("<span>"+messageRefresh+"</span>");
            $("#spanRefresh").attr("class","button btn_yanz_disable");
           timeSecond--;
       }

       showTimmer = setTimeout(function () {
           var messageRefresh =$.newegg.format(dynamicValidate.refreshSpanMessage,timeSecond);
           $("#spanRefresh").html("<span>"+messageRefresh+"</span>");
           timeSecond--;
           if (timeSecond < 0) {
               clearTimeout(showTimmer);
               $("#spanRefresh").attr("class","button btn_yanz");
               $("#spanRefresh").html("<span>"+dynamicValidate.refreshMessage+"</span>");
           } else {
               Biz.AccountCenter.CellPhoneValidation.dynamicMessage(timeSecond);
                $("#spanRefresh").attr("class","button btn_yanz_disable");
           }
       }, 1000);
    },
    create: function(obj,page,isCancelPhone) //Method called on each request
    {     
        if(!Biz.AccountCenter.CellPhoneValidation.checkRefreshGet(dynamicValidate.refreshTimeLimit)) //On each request, js checks the 60s interval
        {
           $("#valiateerror").empty().html("<span class='Validform_wrong'>"+$.newegg.format($Resource.BuildContent("AccountCenter_ModifyDyanmic_CanNotRepeatClick"),dynamicValidate.refreshTimeLimit)+"</span>").show();
           $("#mobilewarning").hide();
           return;
        }
       $.get("url",data,function(){

             //If successful
             Biz.AccountCenter.CellPhoneValidation.dynamicMessage(dynamicValidate.refreshTimeLimit);//The countdown
             Biz.AccountCenter.CellPhoneValidation.resetRefreshGet();//Reset the time
       });
        
    }
 }


Related articles: