21

刻々と変化する時間と価格のサイトを構築しています。私が最も関心を持っているのは、すべてのクライアントで可能な限り正確になるように時刻を同期することです。

現在、クライアントに残りのミリ秒数を送信してからカウントダウン タイマーを起動していますが、転送とレンダリングの遅延により、同じコンピューターで 2 つのブラウザーを使用していても数秒ずれることがあります。

クライアントの JavaScript 時刻とサーバー時刻を同期する方法はありますか? それとも、このわずかな遅れに対処する必要がありますか?

サーバーがデータを送信してから、クライアントが受信してレンダリングするまでの時間差を正確に測定する方法があれば。

4

4 に答える 4

23

サーバーとクライアントの時間は異なりますが、時間の変化率は基本的に同じである必要があります。残り時間をクライアントに送信し、クライアントにその時間を現在の時間に追加させてから、クライアントにそれからカウントダウンを計算させます。例えば:

var timeRemaining = [rendered on page load or queried by ajax]; // in milliseconds
var endTime = new Date(new Date().getTime() + timeRemaining);

// Put this in a setInterval, or however you currently handle it
var countdown = (endTime.getTime() - new Date().getTime()) / 1000;
于 2011-03-18T21:02:45.123 に答える
16

クライアントをサーバーの時間と同期させる方法があります。これを行うライブラリを作成しました:ServerDate

以下は README の一部です。

関数またはそのインスタンスの 1 つをServerDate使用するのと同じように使用できます。Date

> ServerDate()
"Mon Aug 13 2012 20:26:34 GMT-0300 (ART)"

> ServerDate.now()
1344900478753

> ServerDate.getMilliseconds()
22

ServerDate によるサーバーのクロックの推定精度 (ミリ秒単位) を取得する新しいメソッドもあります。

> ServerDate.toLocaleString() + " ± " + ServerDate.getPrecision() + " ms"
"Tue Aug 14 01:01:49 2012 ± 108 ms"

サーバーのクロックとブラウザのクロックの差をミリ秒単位で確認できます。

> ServerDate - new Date()
39
于 2012-08-14T19:54:39.550 に答える
1

サーバー全体のラウンドトリップにかかる時間を測定し、2で割って、時間差の適切な見積もりを取得できます。注意:IPパッケージが両方向で同じルートをたどるという保証はありませんが、その可能性は非常に高いです。

Date.getTime()Millisesondsの解像度で十分な場合に使用できます。

于 2011-03-18T20:56:13.507 に答える
0

これに対する解決策は Javascript です。クライアントのタイム ゾーン設定にアクセスできます。残念ながら、特定の日付のタイム ゾーン オフセット (分単位で指定) しか取得できず、タイム ゾーン名は取得できません。したがって、正しいタイム ゾーンを特定するには、夏時間 (DST) が採用されているかどうかも知る必要があります。これは、ソリューションのクライアント側の部分です。

var now = new Date();
var later = new Date();
// Set time for how long the cookie should be saved
later.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
// Set cookie for the time zone offset in minutes
setCookie("time_zone_offset", now.getTimezoneOffset(), later, "/");
// Create two new dates
var d1 = new Date();
var d2 = new Date();
// Date one is set to January 1st of this year
// Guaranteed not to be in DST for northern hemisphere,
// and guaranteed to be in DST for southern hemisphere
// (If DST exists on client PC)
d1.setDate(1);
d1.setMonth(1);
// Date two is set to July 1st of this year
// Guaranteed to be in DST for northern hemisphere,
// and guaranteed not to be in DST for southern hemisphere
// (If DST exists on client PC)
d2.setDate(1);
d2.setMonth(7);
// If time zone offsets match, no DST exists for this time zone
if(parseInt(d1.getTimezoneOffset())==parseInt(d2.getTimezoneOffset()))
{
setCookie("time_zone_dst", "0", later, "/");
}
// DST exists for this time zone – check if it is currently active
else {
// Find out if we are on northern or southern hemisphere
// Hemisphere is positive for northern, and negative for southern
var hemisphere = parseInt(d1.getTimezoneOffset())-parseInt(d2.getTimezoneOffset());
// Current date is still before or after DST, not containing DST
if((hemisphere>0 && parseInt(d1.getTimezoneOffset())==parseInt(now.getTimezoneOffset())) ||
(hemisphere<0 && parseInt(d2.getTimezoneOffset())==parseInt(now.getTimezoneOffset()))) { setCookie("time_zone_dst", "0", later, "/"); } // DST is active right now with the current date else { setCookie("time_zone_dst", "1", later, "/"); } }

結果を Cookie として保存し、PHP スクリプトからアクセスできます。上記のコードは、少なくともユーザーがアクセスする最初のページに含める必要があります。セッション中にそのような変更が発生する可能性が低い場合でも、変更を認識 (および適応) するために、すべてのページにコードを含めます。

PHP では、PHP 5.1.3 以降で利用可能な timezone_name_from_abbr という名前の新しい関数を使用して、有効なタイム ゾーンを抽出できます。タイム ゾーンの省略形、またはタイム ゾーン オフセット (秒単位) と夏時間の組み合わせのいずれかを使用します。後者の組み合わせ:

$time_zone_name = timezone_name_from_abbr(", -$_COOKIE['time_zone_offset']*60, $_COOKIE['time_zone_dst']);

これにより、Cookie のデータが有効であれば、ユーザーの正しいタイム ゾーン名が得られます。たとえば、「ヨーロッパ/ベルリン」や「ヨーロッパ/チューリッヒ」など、正確な名前を持つ「重複した」名前が多数あることに注意してください。同じタイム ゾーン設定 (少なくとも今のところ) であり、適切なオフセットおよび DST 変数に対してそれらのいずれかを取得できます。タイム ゾーン名のリストは、php.net のサポートされているタイム ゾーンのリストにあります。

特定のタイム ゾーンで日付文字列を作成 する ユーザーのタイム ゾーンの名前を使用して、PHP クラスの DateTimeZone と DateTime を使用して、最終的に正しいタイム ゾーンで日付文字列を作成できます。

// Create time zone class
$time_zone_class = new DateTimeZone($time_zone_name);
// Create new date class with a given date
// Notice that the provided date will be regarded as being in the
// default time zone and converted accordingly
$new_date = new DateTime(‘2007-02-14 15:30:00′, $time_zone_class);
// Print date with the user’s time zone echo $new_date->format(‘Y-m-d H:i:s’);

それでおしまい!

ソース: http://togl.me/eE2

于 2012-02-19T16:47:19.037 に答える