var countdown = { target : new Date(2008, 7, 1), offset : 0, sec : 0, min : 0, hour: 0, days: 0, first: true } countdown.start = function() { window.setInterval("countdown.tick()",100); var now = new Date(); var server = new Date(2012, 1, 7, 2, 02, 14); this.offset = server.getTime() - now.getTime(); this.tick(); } countdown.tick = function() { var now = new Date(); var diff = Math.floor((this.target.getTime() - this.offset - now.getTime())/1000); if (diff < 0 ) diff = 0; var sec = diff % 60; var min = Math.floor((diff / 60) % 60); var hour =Math.floor((diff / 3600) % 24); var days = Math.floor(diff / 86400); // console.log("Tick: %d Days %d Hours %d Mins %d Secs",days,hour,min,sec); this.transition(sec % 10, this.sec % 10, "sec0"); this.transition(sec / 10, this.sec / 10, "sec1"); this.transition(min % 10, this.min % 10, "min0"); this.transition(min / 10, this.min / 10, "min1"); this.transition(hour % 10, this.hour % 10, "hour0"); this.transition(hour / 10, this.hour / 10, "hour1"); this.transition(days % 10, this.days % 10, "days0"); this.transition(days / 10, this.days / 10, "days1"); this.sec = sec; this.min = min; this.hour = hour; this.days = days; this.first = false; } countdown.transition = function(to, from, id) { var f = Math.floor(from); var t = Math.floor(to); var img = document.getElementById(id); if (!img) return; if (t != f) { //console.log("Transition in %s %d => %d",id,f,t); img.src = "img/"+id+"/"+f+"-"+t+".gif"; } if (this.first) { //Initialisieren f = (t + 1) % 10; img.src = "img/"+id+"/"+f+"-"+t+".gif"; //console.log("Init %d-%d",f,t); } }