私も最近この問題に遭遇しました。私の場合、時間の変更は重大な経済的損失を引き起こす可能性があるため、真剣に受け止めなければなりませんでした。
背景:
setTimeout()
IE と Opera は、システムの時間変更を適切に処理します ( 、 への副作用はありませんsetInterval()
)。
- FF < 4.0 およびすべての Webkit ベースのブラウザーは、過去への時間の変更を処理できません (まさにあなたが説明したとおり)。注: Chrome のタイマーは、2 ~ 60 秒後にしか停止しません。
私の解決策: ユーザー インタラクション (つまり、、など) からのイベントを使用してmousemove
、mouseover
時間変更チェックをトリガーします。
Clock.prototype.checkLocalTime = function(){
var diff = Date.now()- this.lastTimestamp;
if ( diff < 0 || diff > 20000) { // if time shifted back or more than 20s to the future
console.warn('System time changed! By ' + diff + 'ms' );
this.restartTimer();
this.getServerTime();
}
this.lastTimestamp = time;
}
お気づきのとおり、タイマーを再起動し、さらに時刻をサーバーと同期します。