服务器时间的倒计时。
直接上代码:
[code lang=”js”]
document.write(‘<form name="formnow" class="formnow mjzt">’);
document.write(‘<input type="text" name="dd" class="dd" value="" disabled>’);
document.write(‘<input type="text" name="hh" class="hh" disabled>’);
document.write(‘<input type="text" name="mm" class="mm" disabled>’);
document.write(‘<input type="text" name="ss" class="ss" id="ss" disabled>’);
document.write(‘</form>’);
var DifferenceHour = -1
var DifferenceMinute = -1
var DifferenceSecond = -1
var Tday = new Date("Mon Dec 24 2012 09:00:00 GMT+0800")
var daysms = 24 * 60 * 60 * 1000
var hoursms = 60 * 60 * 1000
var Secondms = 60 * 1000
var microsecond = 1000
function clock()
{
var time = new Date(parseInt(servertime) * 1000)//服务器时间。自己获取。是一个时间戳。
//var time = new Date()
//alert(servertime);
var hour = time.getHours()
var minute = time.getMinutes()
var second = time.getSeconds()
var timevalue = ""+((hour > 12) ? hour-12:hour)
timevalue +=((minute < 10) ? ":0":":")+minute
timevalue +=((second < 10) ? ":0":":")+second
timevalue +=((hour >12 ) ? " PM":" AM")
// document.formnow.now.value = timevalue
var convertHour = DifferenceHour
var convertMinute = DifferenceMinute
var convertSecond = DifferenceSecond
var Diffms = Tday.getTime() – time.getTime()
if(Diffms>=0){
DifferenceHour = Math.floor(Diffms / daysms)
Diffms -= DifferenceHour * daysms
DifferenceMinute = Math.floor(Diffms / hoursms)
Diffms -= DifferenceMinute * hoursms
DifferenceSecond = Math.floor(Diffms / Secondms)
Diffms -= DifferenceSecond * Secondms
var dSecs = Math.floor(Diffms / microsecond)
if(convertHour != DifferenceHour) document.formnow.dd.value=DifferenceHour
if(convertMinute != DifferenceMinute) document.formnow.hh.value=DifferenceMinute
if(convertSecond != DifferenceSecond) document.formnow.mm.value=DifferenceSecond
document.formnow.ss.value=dSecs
++servertime;
setTimeout("clock()",1000);
}else{
//alert("时间到");
}
// document.formnow.Tnow.value= DifferenceHour DifferenceMinute + DifferenceSecond + dSecs
}
jQuery(function(){clock()});//执行
[/code]
另一个版本:
[code lang=”js”]
<!–倒计时–>
var DifferenceHour = -1
var DifferenceMinute = -1
var DifferenceSecond = -1
var Tday = new Date(1370520373*1000);//设置倒计时
//alert(Tday)
var stday = new Date(1370509573*1000);
var daysms = 24 * 60 * 60 * 1000
var hoursms = 60 * 60 * 1000
var Secondms = 60 * 1000
var microsecond = 1000
var servertime = 1370509867;//当前时间从服务器获取
var fulltime = Tday.getTime() – stday.getTime();//结束时间减去开始时间,得到总时间
function clock()
{
var time = new Date(parseInt(servertime) * 1000);
//var time = new Date()
var hour = time.getHours()
var minute = time.getMinutes()
var second = time.getSeconds()
var timevalue = ""+((hour > 12) ? hour-12:hour)
timevalue +=((minute < 10) ? ":0":":")+minute
timevalue +=((second < 10) ? ":0":":")+second
timevalue +=((hour >12 ) ? " PM":" AM")
// document.formnow.now.value = timevalue
var convertHour = DifferenceHour
var convertMinute = DifferenceMinute
var convertSecond = DifferenceSecond
var Diffms = Tday.getTime() – time.getTime();
if(Diffms>=0){
/*进度条 js 不用进度条可删除*/
//4位小数点 精确计算 //toFixed(6)
var box_w = (Diffms/fulltime)*100;
//alert(box_w);
//document.formnow.test.value=str;
document.getElementById("jt_lding_show").style.width=box_w+"%";
if(box_w==100)alert(‘由于您是未成年人,将被纳入防沉迷系统,3小时内收益经验正常,3小时后游戏角色将会自动下线’);//addClass(document.getElementById("jt_r"), "full");
if(box_w==66)alert(‘您已累计在线1小时,请合理安排游戏时间’);
if(box_w==33)alert(‘您已累计在线2小时,请合理安排游戏时间’);
if(box_w==0){
alert(‘您已累计在线3小时,您已进入疲劳游戏时间,请您下线休息’);
showtextlist(1);
}
/*进度条 js 不用进度条可删除*/
DifferenceHour = Math.floor(Diffms / daysms)
Diffms -= DifferenceHour * daysms
DifferenceMinute = Math.floor(Diffms / hoursms)
Diffms -= DifferenceMinute * hoursms
DifferenceSecond = Math.floor(Diffms / Secondms)
Diffms -= DifferenceSecond * Secondms
var dSecs = Math.floor(Diffms / microsecond)
if(convertHour != DifferenceHour && DifferenceHour>=1){
var ds = DifferenceHour==1 ? " Day":" Days";
document.formnow.dd.value=DifferenceHour+ds;
document.getElementById("hh").style.display="none";
document.getElementById("mm").style.display="none";
document.getElementById("ss").style.display="none";
}
if(convertMinute != DifferenceMinute && DifferenceHour<1 && DifferenceMinute>=1){
var ds = DifferenceMinute==1 ? " 小时":" 小时";
document.formnow.hh.value=DifferenceMinute+ds;
document.getElementById("dd").style.display="none";
document.getElementById("hh").style.display="";
//document.getElementById("mm").style.display="none";
document.getElementById("ss").style.display="none";
}
if(convertSecond != DifferenceSecond && DifferenceHour<1 &&DifferenceSecond>=1){
var ds = DifferenceSecond==1 ? " 分钟":" 分钟";
document.formnow.mm.value=DifferenceSecond+ds;
if(DifferenceMinute<1)document.getElementById("hh").style.display="none";
document.getElementById("dd").style.display="none";
document.getElementById("mm").style.display="";
document.getElementById("ss").style.display="none";
}else if(DifferenceHour<1 && DifferenceMinute<1 && DifferenceSecond<1){
var ds = dSecs<=9 ? " 秒":" 秒";
document.formnow.ss.value=dSecs+ds;
document.getElementById("ss").style.display="";
document.getElementById("hh").style.display="none";
document.getElementById("dd").style.display="none";
document.getElementById("mm").style.display="none";
}
++servertime;
setTimeout("clock()",1000);
}else{
//document.formnow.ss.value="您是未成年人,健康游戏时间已经结束。";
document.formnow.ss.value="0";
showtextlist(1);
//$(".unityPlayer_").hide();
}
// document.formnow.Tnow.value= DifferenceHour DifferenceMinute + DifferenceSecond + dSecs
}
/*进度条 js 不用进度条可删除*/
function hasClass(ele,cls) {
return ele.className.match(new RegExp(‘(\\s|^)’+cls+'(\\s|$)’));
}
function addClass(ele,cls) {
if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
/*进度条 js 不用进度条可删除*/
<!–倒计时–>
[/code]
另:
[code]
<div class="daojishibox" style="display:none">
<div class="daojishi" style="height:126px;">
<div class="days one_day">00</div>
<div class="hours_one one_day">00</div>
<div class="min_one one_day">00</div>
<div class="src_one one_day">00</div>
</div>
<script>
var DifferenceHour = -1
var DifferenceMinute = -1
var DifferenceSecond = -1
var Tday = new Date("Wed Dec 4 2013 21:00:00 GMT-0500")
//alert(Tday);
var daysms = 24 * 60 * 60 * 1000
var hoursms = 60 * 60 * 1000
var Secondms = 60 * 1000
var microsecond = 1000
//var servertime = 1386049054;
var servertime=<{$servertime}>;
function clock()
{
var time = new Date(parseInt(servertime) * 1000)
//var time = new Date()
//alert(servertime);
//navigator.userAgent.indexOf("Firefox")
if(navigator.userAgent.indexOf("Chrome")>0){
}
var hour = time.getHours()
var minute = time.getMinutes()
var second = time.getSeconds()
var timevalue = ""+((hour > 12) ? hour-12:hour)
timevalue +=((minute < 10) ? ":0":":")+minute
timevalue +=((second < 10) ? ":0":":")+second
timevalue +=((hour >12 ) ? " PM":" AM")
// document.formnow.now.value = timevalue
var convertHour = DifferenceHour
var convertMinute = DifferenceMinute
var convertSecond = DifferenceSecond
var Diffms = Tday.getTime() – time.getTime()
if(Diffms>=0){
DifferenceHour = Math.floor(Diffms / daysms)
Diffms -= DifferenceHour * daysms
DifferenceMinute = Math.floor(Diffms / hoursms)
Diffms -= DifferenceMinute * hoursms
DifferenceSecond = Math.floor(Diffms / Secondms)
Diffms -= DifferenceSecond * Secondms
var dSecs = Math.floor(Diffms / microsecond)
if(convertHour != DifferenceHour) $(".daojishi .days").html(DifferenceHour);
if(convertMinute != DifferenceMinute) $(".daojishi .hours_one").html(DifferenceMinute);
if(convertSecond != DifferenceSecond) $(".daojishi .min_one").html(DifferenceSecond);
$(".daojishi .src_one").html(dSecs);
//alert(DifferenceHour)
++servertime;
setTimeout("clock()",1000);
}else{
$(".login_reg_play").addClass("play");
$(".play_nowflash,.daojishibox").toggle();
}
}
clock()
</script>
</div>
[/code]
附: