Calculate countdown by reading X-FIC-time giving by the server instead of rely on user system date

This commit is contained in:
nemunaire 2014-11-23 16:56:30 +01:00
commit 5d7967a157
5 changed files with 29 additions and 18 deletions

View file

@ -1,24 +1,34 @@
function update_end()
function update_end(server_time, receive_date)
{
var left;
if (typeof end_challenge === 'undefined')
left = 14400;
else
left = (end_challenge - new Date())/1000;
var elapsed = new Date() - receive_date;
if (left < 0) left = 0;
var left = 10800;
if (typeof end_challenge !== 'undefined')
left = (end_challenge - server_time - elapsed)/1000;
var heure = Math.floor(left / 3600);
var min = Math.floor((left / 60) % 60);
var sec = Math.floor(left % 60);
if (left < 0) left = 0;
$("#hours").html(( heure < 10 ? "0" : "" ) + heure);
$("#min").html(( min < 10 ? "0" : "" ) + min);
$("#sec").html(( sec < 10 ? "0" : "" ) + sec);
var heure = Math.floor(left / 3600);
var min = Math.floor((left / 60) % 60);
var sec = Math.floor(left % 60);
$("#hours").html(( heure < 10 ? "0" : "" ) + heure);
$("#min").html(( min < 10 ? "0" : "" ) + min);
$("#sec").html(( sec < 10 ? "0" : "" ) + sec);
}
function getHeader(name, def) {
for (var j = 0; j < getHttp.length(); j++) {
if (getHttp.header(j) == name)
return getHttp.data(j);
}
return def;
}
$(document).ready(function() {
setInterval( function() {
update_end();
}, 1000);
server_time = new Date(getHeader("X-FIC-time"));
receive_date = new Date();
setInterval( function() {
update_end(server_time, receive_date);
}, 1000);
});