0

重複の可能性:
時計 (日付と時刻) を 4 倍速く実行する Javascript

指定した時間値 (hh:mm:ss) で開始し、4 倍の速度で実行される時計を作成しようとしています (4 倍の実時間で実行されるオンライン ゲームのサーバー時間)。これを行うためにオンラインで見つけた無料の時計を変更しましたが、1分おきにしか機能しません(意味がない場合は、以下のコードを試して正確に確認してください)。

var customClock = (function () {
    var timeDiff;
    var timeout;

    function addZ(n) {
        return (n < 10 ? '0' : '') + n;
    }

    function formatTime(d) {
        t1 = d.getHours();
        t2 = d.getMinutes();
        t3 = d.getSeconds() * 4;
        if (t3 > 59) {
            t3 = t3 - 60;
            t2 = t2 + 1;
        }
        if (t2 > 59) {
            t2 = t2 - 60;
            t1 = t1 + 1;
        }
        if (t1 > 23) {
            t1 = 0;
        }
        return addZ(t1) + ':' + addZ(t2) + ':' + addZ(t3);
    }

    return function (s) {
        var now = new Date();
        var then;

        var lag = 1015 - now.getMilliseconds();

        if (s) {
            s = s.split(':');
            then = new Date(now);
            then.setHours(+s[0], +s[1], +s[2], 0);
            timeDiff = now - then;
        }

        now = new Date(now - timeDiff);
        document.getElementById('clock').innerHTML = formatTime(now);
        timeout = setTimeout(customClock, lag);
    }
}());

window.onload = function () {
    customClock('00:00:00');
};

なぜこれが起こっているのですか?私はJavascriptにかなり慣れていないので、これは間違いなく少しハックです。ありがとう

4

3 に答える 3

2

私は元の時間を取り、それを現在の時間から引き、それを 4 倍して元の時間に加算します。同期の問題に注意する必要があると思います。

(function(){
  var startTime = new Date(1987,08,13).valueOf() //save the date 13. august 1987
    , interval = setInterval(function() {
          var diff = Date.now() - startTime

          //multiply the diff by 4 and add to original time
          var time = new Date(startTime + (diff*4))

          console.log(time.toLocaleTimeString())
      }, 1000)
}())

カスタム日付での使用方法( Dateオブジェクトを使用)

Date(year, month, day, hours, minutes, seconds, milliseconds)

于 2013-01-21T02:25:21.760 に答える
0

var lag = 1015 - now.getMilliseconds();は、「次のクロックティックの後に、これをもう一度実行する(15ミリ秒)」ことを試みています。この値を小さくすると(4で割る?)、このコードはより頻繁に実行されます。

次に、現在の時計の長さの4倍を表示します。同様の問題:formatTime()の内部または外部のいずれかで現在の詳細に4を掛ける

于 2013-01-21T02:14:22.367 に答える
0

最初Clockに次のようにコンストラクターを作成します。

function Clock(id) {
    var clock = this;
    var timeout;
    var time;

    this.hours = 0;
    this.minutes = 0;
    this.seconds = 0;
    this.stop = stop;
    this.start = start;

    var element = document.getElementById(id);

    function stop() {
        clearTimeout(timeout);
    }

    function start() {
        timeout = setTimeout(tick, 0);
        time = Date.now();
    }

    function tick() {
        time += 1000;
        timeout = setTimeout(tick, time - Date.now());
        display();
        update();
    }

    function display() {
        var hours = clock.hours;
        var minutes = clock.minutes;
        var seconds = clock.seconds;

        hours = hours < 10 ? "0" + hours : "" + hours;
        minutes = minutes < 10 ? "0" + minutes : "" + minutes;
        seconds = seconds < 10 ? "0" + seconds : "" + seconds;

        element.innerHTML = hours + ":" + minutes + ":" + seconds;
    }

    function update() {
        var seconds = clock.seconds += 4;

        if (seconds === 60) {
            clock.seconds = 0;
            var minutes = ++clock.minutes;

            if (minutes === 60) {
                clock.minutes = 0;
                var hours = ++clock.hours;

                if (hours === 24) clock.hours = 0;
            }
        }
    }
}

次に、時計を作成して、次のように開始できます。

var clock = new Clock("clock");
clock.start();

ここにデモがあります:http://jsfiddle.net/Nt5XN/

于 2013-01-21T02:44:58.913 に答える