首頁  >  文章  >  web前端  >  js判斷60秒以及倒數範例程式碼_javascript技巧

js判斷60秒以及倒數範例程式碼_javascript技巧

WBOY
WBOY原創
2016-05-16 17:02:451042瀏覽

用js判斷60秒到計時,先定義一個變數refreshTime,當觸發事件的時候檢查一下上一次請求成功的時間距離現在是否超過60秒,如果沒有超過60秒,則彈出提示,

否則則允許繼續請求,請求成功之後,把當前的時間賦值給refreshTime,具體實現方式;

複製代碼 程式碼如下:

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(){

           essage(dynamicValidate.refreshTimeLimit);//倒數計時
             Biz .AccountCenter.CellPhoneValidation.resetRefreshGet();//重置時間
       });
       
 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn