Hello,

I'm currently developping a site that needs to display consistent timers across clients, so I need to initialize client timers with the server time. I figured the "current_server_time" argument would enable me to do just that, but I'm having trouble using it. The thing is I'm adding timers as fields in content types using the field API.

I quite don't get how I could dynamically pass the server time to timer widgets in nodes of the relevant type whenever they're being displayed.

Maybe I should turn the problem on its head and have the widget javascript fetch server time when needed?

Thing is, I will potentially have to show 10 or 15 timers on a single page, and I wouldn't like the server to have to handle that many unnecessary requests. I was thinking I could have a first instance of the widget fetch server time once, compute the difference between this and the local time and put it in a global javascript variable for all the other widget instances to use.

But then I'd have to wait for the server to respond before I allow all the timers to be displayed. And take the server response time into account.

This is giving me headaches. Could someone please point me to the right direction?

Thanks a million.

-tkz

Comments

jvandervort’s picture

To do something like that you might have to use the php filter (module that has to be enabled) which allows you to write oho code in a node or write your own theme templates that would inject the server data including the timers. A custom module would work as well.

jstimer doesn't have any of this built into it. It will just move the timer after you have the correct html showing on your page.

Ace Cooper’s picture

Version: 7.x-1.3 » 7.x-1.x-dev
Category: support » feature

I'm currently looking for the same feature of server-synced timer.

My problem with JSTimer is as follows:
If the user-side time differs from the server-side time (user does not have a NTP-sycned time) - the timer will finish earlier or later than the actual trigger/rule for the node with the timer. This is very bad for UX as users expect the timer to be accurate.

My ideas for a solution:
Format the date field to display TIME LEFT - difference between server-side NOW and the actual value of the date field.
Then use jQuery script to substract seconds from the displayed time according to client-side clock.
The timer error will be marginal in this case, also every page reload will sync field time with the server time.

I will pursue this goal and see if I can make JSTimer behave according to my needs.
Any ideas and suggestions are welcome.

P.S. Futher ideas include:
1) make jstimer fire events to the php backend (completion and approach warnings);
2) make jstimer trigger cron or specific rules within Drupal;
3) make jstimer update via ajax, etc.

P.P.S. The formatter for the TIME LEFT field should look something like this (for PHP 5.3):

<?php
       $now = new DateTime();
       $expiration = new DateTime($node->field_expiration['und'][0]['value']);
       $remaining = $expiration->diff($now);
       print $remaining->format("%d days, %h hours, %i minutes");
?>
jvandervort’s picture

Jstimer does have support for interval seconds as well. Then the time sync problem would go away.

<span class="jst_timer">
 <span style="display:none" class="interval">10500</span>
 <span class="format_txt" style="display:none;">%hours%::%mins%::%secs%</div>
</span>

The server would have to supply the proper interval on page load, and be wary of (or disable) page caching or back button usage.

Ace Cooper’s picture

@ jvandervort: excelent, thank you for the hint on the "interval" option.
I managed to make my timers run consistent to the server-time for every client in the following way:

1) Used Date module in my content type to collect the End date.

2) With Custom Formatters module I created a formatter (PHP) for the End date, which converts the "datetime" format into a UNIX timestamp and calculated the difference between it and server-side NOW: http://take.ms/CSVuEd

$end = strtotime($variables['#items'][0]['value2']);
$now = date_timestamp_get(new DateTime());
echo $end-$now;

3) In Views my End date is displayed with the formatter created above, but I also rewrite the field output to add the JSTimer wrapping:

<span class="jst_timer">
<span style="display:none" class="interval">[quote_period]</span>
<span class="format_txt" style="display:none;">%days% д. %hours%:%mins%:%secs%</span>
</span>

Now the timer output does not depend on the user-side time for accuracy.
The amount of seconds left is calculated based on the server clock.
Works like a charm: http://take.ms/kGVkN

jvandervort’s picture

Issue summary: View changes
Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.