本篇文章主要是對js判斷60秒以及倒計時的示例代碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
用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<timeLimit ) { 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("<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) //每次請求調用的方法 { if(!Biz.AccountCenter.CellPhoneValidation.checkRefreshGet(dynamicValidate.refreshTimeLimit)) // 每次請求的時候,js檢查60s時間間隔 { $("#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(){ //如果成功 Biz.AccountCenter.CellPhoneValidation.dynamicMessage(dynamicValidate.refreshTimeLimit);// 倒計時 Biz.AccountCenter.CellPhoneValidation.resetRefreshGet();//重置時間 }); } }