$( document ).ready(function() { fetchEndTime(); $.ajax({ url: 'countdown-widget/timer_configuration.php', type: "GET", data : {'detail' : 'detail'}, success: function(data){ $('#event-time').html(data); } }); $("#countdownWidget").click(function(){ window.location=$(this).find("a").attr("href"); return false; }); }); function fetchEndTime() { $.ajax({ url: 'countdown-widget/timer_configuration.php', type: "GET", data : {'exp' : 'exp'}, success: function(data){ clock(data); } }); } function clock(expire) { // Count down milliseconds = server_end - server_now = client_end - client_now var server_end = expire * 1000; var server_now = 1714349035 * 1000; var client_now = new Date().getTime(); var end = server_end - server_now + client_now; // this is the real end time var _second = 1000; var _minute = _second * 60; var _hour = _minute * 60; var _day = _hour *24 var timer; function showRemaining() { var now = new Date(); var distance = end - now; if (distance < 0 ) { clearInterval( timer ); fetchEndTime(); } var days = Math.floor(distance / _day); var hours = Math.floor( (distance % _day ) / _hour ); var minutes = Math.floor( (distance % _hour) / _minute ); var seconds = Math.floor( (distance % _minute) / _second ); positions = $('#countdownWidget').find('.position'); updateDuo(0, 1, days); updateDuo(2, 3, hours); updateDuo(4, 5, minutes); updateDuo(6, 7, seconds); function updateDuo(minor,major,value){ switchDigit(positions.eq(minor),Math.floor(value/10)%10); switchDigit(positions.eq(major),value%10); } } timer = setInterval(showRemaining, 1000); } function switchDigit(position,number){ var digit = position.find('.digit') if(digit.is(':animated')){ return false; } if(position.data('digit') == number){ // We are already showing this number return false; } position.data('digit', number); var replacement = $('',{ 'class':'digit', css:{ top:'-2.1em', opacity:0 }, html:number }); // The .static class is added when the animation // completes. This makes it run smoother. digit .before(replacement) .removeClass('static') .animate({top:'2.5em',opacity:0},'fast',function(){ digit.remove(); }) replacement .delay(100) .animate({top:0,opacity:1},'fast',function(){ replacement.addClass('static'); }); }