js 计时器,时间与服务器同步

var w=top.window, d=w.document;
var game_version = '1.733';
var can_act = 0;
var IE_all_cache = new Object();
w.rd=Math.ceil(Math.random()*1000000);
w.hookedFunctions = [];

function trim(vStr)
{
    return vStr.replace(/(^[/s]*)|([/s]*$)/g,"");
}

var ucalDate = function (mdate) {
    var mt = "";
    var cx = new Date();
    this.cmktime = Math.ceil(cx.getTime()/1000);
    if (!this.cmktime){this.cmktime=0}
    if (!mdate || mdate == "") {
        var cy = cx.getFullYear();
        var cm = cx.getMonth()+1;
        var cd = cx.getDate();
        var ch = cx.getHours();
        var ci = cx.getMinutes();
        var cs = cx.getSeconds();
        if (cm < 10) cm = "0"+cm;
        if (cd < 10) cd = "0"+cd;
        if (ch < 10) ch = "0"+ch;
        if (ci < 10) ci = "0"+ci;
        if (cs < 10) cs = "0"+cs;
        mdate = cy+'-'+cm+'-'+cd+' '+ch+':'+ci+':'+cs;
    }
    this.date = mdate;
    this.year = mdate.substring(0,4);
    this.month = mdate.substring(5,7);
    this.day = mdate.substring(8,10);
    this.hours = mdate.substring(11,13);
    this.minutes = mdate.substring(14,16);
    this.seconds = mdate.substring(17,19);
    cx = new Date(this.year*1, this.month*1-1, this.day*1, this.hours*1, this.minutes*1, this.seconds*1);
    this.smktime = Math.ceil(cx.getTime()/1000);
    if (!this.smktime){this.smktime=0}
    this._mktime = (1*this.smktime)-(1*this.cmktime*1);  //计算时间差值
}
w.ucal_cdate = new ucalDate();
w.ucal_sdate = w.ucal_cdate;

function mainFunction() {              
    w.ucal_cdate = new ucalDate();  //这个很重要
    for (var ii = 0; ii<w.hookedFunctions.length; ii++) eval(w.hookedFunctions[ii]);
}                  
           
function MM_setClientTime() {
   
    w.ucal_sdate = new ucalDate($("systime").value); //输入服务器时间
}

function MM_timeHook() {
   
    var myes=null;
    if ((myes=document.getElementsByName("timeHook"))==null) return;
    var myel = myes.length;
    for(var ii=0; ii<myel; ii++) {
        var mye = myes[ii];
        tasktime = new ucalDate(mye.getAttribute('sgtitle'));
        var eTime = tasktime.smktime;
        var iTime =eTime- (w.ucal_sdate._mktime + w.ucal_cdate.cmktime);  //计算差值 注意两个不同对象的属性
        refreshact = mye.getAttribute('act');
       
        if (iTime>0) {
            hTime = Math.floor(iTime/3600);
            mTime = Math.floor((iTime%3600)/60);
            sTime = iTime%60;
            mye.innerHTML = ((hTime<10) ? ("0" + hTime) : hTime) + ":" + ((mTime<10) ? ("0"+mTime) : mTime) + ":" + ((sTime<10) ? ("0"+sTime) : sTime);
        } else if (iTime<0) {
            mye.name = '_timeHook';
            mye.innerHTML='00:00:00';
        } else {
            mye.innerHTML='<font height:20px;text-line:20px;width:80px;>00:00:00</font>';
            //MM_refreshTimehook(refreshact);
            //MM_Nav(0);
        }
    }
}
function addFunctionHook(functionName) {
    w.hookedFunctions[w.hookedFunctions.length]=functionName;
}
function MM_bodyOnload() {
   
    if (typeof($('timeHook'))!="undefined") {
        addFunctionHook('MM_timeHook()');
        MM_setClientTime();
        w.globalClock = w.setInterval("mainFunction()", 1000);
    }
}

(function(){
    function G(){
        if (window.addEventListener) {
            window.addEventListener('load', MM_bodyOnload, false);
        } else if (window.attachEvent) {
            window.attachEvent('onload', MM_bodyOnload);
        }
    };
    G();
})();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值