0

I have a countdown timer using http://keith-wood.name/countdown.html Jquery Countdown plugin.

There is a problem when the client computer time is not set correctly (ie: out of synched or misconfigures).

Here I would like to achieve a trusted countdown regardless of the client-side time but I couldn't figured out how I can achieve it. (from where I can fetch correct time and how to calculate and offset the correction in the countdown?)

Edit: Let me clarify a little bit more. I do not want milliseconds precision (actually 1 sec precision if enough for me). Also, I do not want to keep client to server synchronized all the time.. I only need to calculate how much client computer time has an offset at the page load. Also I need your help for the modified version of my countdown script.. I can create a JSON API on the backend to serve time if needed (but please guide me what format I shall serve.. My problem is mainly with JS)

Below you can find my countdown time code. I will appreciate any kind of support.

$('#remaining_time').countdown('2020-01-01 00:00:00 +0300')
.on('update.countdown', function(event) {
    var format = '%S sn';
    if(event.offset.weeks > 0) {
        format = '%-w Week +';
    } else {
        if (event.offset.totalDays > 0) {
            format = '%-D Days +';
        } else { if (event.offset.totalHours > 0) {
                format = '%H Hours, %M min, %S sec';
            } else { if (event.offset.totalMinutes > 0) {
                                format = '%M min, %S sec';
                                }
            }
        }
    };
    $(this).html(event.strftime(format));
})
.on('finish.countdown', function(event) {
    $(this).html('Time is Up!');
});
Tolga
  • 1,307
  • 3
  • 16
  • 31
  • Not trivial. There are [algorithms for this](https://en.wikipedia.org/wiki/Clock_synchronization#Problems) but don't expect miracles. Perhaps a stopgap would be to have your server emit WebSocket events on every second. If this is a game of some sort, you can have the client do the countdown and independently validate on the server the server time you got the response from the client, then check whether the response is within the allowed timeframe. – nicholaswmin Nov 30 '18 at 18:24
  • https://stackoverflow.com/questions/13240530/free-rest-api-to-retrieve-current-datetime-as-string-timezone-irrelevant – Jonas Wilms Nov 30 '18 at 18:26

0 Answers0