私はもののようなもののために書いたクラスを追加しました:私はここではサンプルです、あなたがGoogleでそれを見つけることができると確信していますが、...。 このデモでは、秒数を入力して進捗状況を確認しました。
/*
\t a (pausable) linear equation over real time
\t
\t \t value = _speed * Date.now() + _offset; //+ pausing logic
\t \t
\t so basically a clock, a stopwatch, a countdown, a gauge, ...
\t since it is only a linear equation over time, it is independant of any interval.
\t It computes the value (using Date.now()) whenever you ask for it. Wether this is ever frame or every hour.
*/
class Clock {
\t constructor(value=Date.now(), speed=1){
\t \t //state; changes only when YOU set one of the properties (value, paused or speed)
\t \t this._offset = +value || 0;
\t \t this._speed = +speed || 0;
\t \t this._paused = true;
\t \t
\t \t //preparing a simple hook to get notified after the state has been updated (maybe to store the new state in the localStorage)
\t \t this.onStateChange = undefined;
\t }
\t
\t get value(){
\t \t return this._paused? this._offset: this._speed*Date.now() + this._offset
\t }
\t set value(arg){
\t \t let value = +arg || 0;
\t \t let offset = this._paused? value: value - this._speed * Date.now();
\t \t \t
\t \t if(this._offset !== offset){
\t \t \t this._offset = offset;
\t \t \t if(typeof this.onStateChange === "function")
\t \t \t \t this.onStateChange(this);
\t \t }
\t }
\t
\t get speed(){
\t \t return this._speed
\t }
\t set speed(arg){
\t \t let speed = +arg || 0;
\t \t if(this._speed !== speed){
\t \t \t if(!this._paused)
\t \t \t \t this._offset += Date.now() * (this._speed - speed);
\t \t \t this._speed = speed;
\t \t \t if(typeof this.onStateChange === "function")
\t \t \t \t this.onStateChange(this);
\t \t }
\t }
\t
\t get paused(){
\t \t return this._paused
\t }
\t set paused(arg){
\t \t let pause = !!arg;
\t \t if(this._paused !== pause){
\t \t this._offset += (pause? 1: -1) * this._speed * Date.now();
\t \t \t this._paused = pause;
\t \t \t if(typeof this.onStateChange === "function")
\t \t \t this.onStateChange(this);
\t \t }
\t }
\t time(){
\t \t let value = this.value,v = Math.abs(value);
\t \t return {
\t \t \t value,
\t \t \t //sign: value < 0? "-": "",
\t \t \t seconds: Math.floor(v/1e3)%60,
\t \t \t minutes: Math.floor(v/6e4)%60,
\t \t \t hours: Math.floor(v/36e5)%24,
\t \t \t days: Math.floor(v/864e5)
\t \t }
\t }
\t
\t valueOf(){
\t \t return this.value;
\t } \t
\t
\t start(){
\t \t this.paused = false;
\t \t return this; \t \t
\t }
\t stop(){
\t \t this.paused = true;
\t \t return this;
\t }
}
function lz(v){ //leading zero
return String(v).padStart(2, 0);
}
function update(){
let {hours, minutes, seconds} = clock.time();
let node = document.getElementById('clock');
node.textContent = [hours, minutes, seconds].map(lz).join(":");
requestAnimationFrame(update);
//setTimeout(update, 250);
}
let clock = new Clock(Date.now(), 4).start();
update();
<h1 id="clock"></h1>
これはとても良いですありがとう、問題はどのように私はその機能を追加することになります。これ時計が現地時間からそれぞれの負荷を開始し続けるとはならないということですか?
私はonStateChange
フックを追加しました。 このクロックの状態を変更したときに保存するために使用することができます。
let clock = new Clock(Date.now(), 4).start();
//add the hook
clock.onStateChange = function(){
//will be called whenever you change the state of this clock. things like changing the speed or paused state, or when you set the value.
localStorage.setItem("clock", JSON.stringify(this));
}
//check wether there is a stored state for this "clock"
if(localStorage.hasItem("clock")){
//simply overwrite the state of the clock.
Object.assign(clock, JSON.parse(localStorage.getItem("clock")))
}else{
//if there's no current state stored, force storing the current state
clock.onStateChange();
}
私はここにSO上でこれらのスニペットにlocalStorage
へのアクセス権を持っていないので、私は別々にこのコードを追加しました。
ページが読み込まれてそこから4倍速で移動する正しい時刻として開始する必要がありますか? – arbuthnott
@arbuthnottいいえ、始めるべきではありません。正しい時間です。偽の時間を過ごして、偽の時間を過ごすべきです。 – Thea