您的位置:首页 > Web前端 > JavaScript

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

2011-01-27 13:20 225 查看
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();

})();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: