10 分ごとに呼び出す必要がある関数を含むスクリプトで作業しています。また、関数をリロードするまでの残り時間をユーザーに通知するために (AJAX 経由ですが、これは今は関係ありません)、少し回帰タイマーを入れました。
これは主な構造です:
$(document).ready(function () {
// Test's function
function loadDate() {
var myDate = new Date();
$("#dateload").html(myDate);
};
// Startup Variables
countermin = 10; // 10 minutes
countersec = 60;
minpass = 0;
secpass = 0
// Showing info before timer.
$("#reloadtime").html("Function will be reloaded in " + countermin + "m<b>" + (60 - countersec) + "</b>s<br/>(countersec's value: " + countersec + " - secpass' value: " + secpass + ")");
$("#timescalled").text("The date function has been called " + minpass + " times after page's load.");
loadDate();
// FIRST setInterval
// It's our timer.
intvl1 = setInterval(function () {
if (countersec++ == 60) {
countermin--;
countersec = 1;
}
if (countermin < 0) {
countermin = 9;
countersec = 1;
secpass = 0;
}
secpass++;
$("#reloadtime").html("Function will be reloaded in " + countermin + "m<b>" + (60 - countersec) + "</b>s<br/>(countersec's value: " + countersec + " - secpass' value: " + secpass + ")");
}, 1000);
// SECOND setInterval
// Counting 10 minutes to execute the function again (and again...).
intvl2 = setInterval(function () {
minpass++;
$("#minpass").text("The date function has been called " + minpass + " times after page's load.");
loadDate();
}, 600000);
});
私の問題は、両方のタイマーが同期されていないことです。はintvl2
機能を実行し、タイマー ( ) が 0 になる前に戻りintvl1
ます。エラーは約 20 秒で、10 分ごとに増加します。
実行開始から6、7分あたりに表示されている時間と比べてみると、PCの時計と比べると時間の差がわかります。
どうすればそれらを同期させることができますか?
このフィドルを確認できます。