2

数秒ごとに div を更新する単純なシステムがあります。

$(document).ready(function() {
         $("#nownext").load("response.php");
       var refreshId = setInterval(function() {
          $("#nownext").load('response.php?randval='+ Math.random());
       }, 20000);
    });

さて、コンテンツの性質上、正時または 30 時半に更新される可能性が高くなります。(常にではありませんが)。私がやりたいことは、システムをより正確にするために、正時から数分後 (および半時) の間により頻繁に更新することです。

これは可能ですか?クライアントのコンピューターに過度のストレスを与えずに行うにはどうすればよいですか?

4

2 に答える 2

1

次の間隔のタイミングを動的に変更できるように、setInterval の代わりに setTimeout を使用します。「高速」期間に Date() オブジェクトをミリ秒単位で作成およびチェックすることのパフォーマンスへの影響はわかりませんが、問題が発生した場合は、常にその頻度を毎秒近くに調整できます。

start_timer = function(timing) {
   var timer, d = new Date(), min = d.getMinutes(),
     timeout = 20000; /* slow timeout */
   if((min >= 28 && min <= 30) || min >= 58) {
     timeout = 100; /* fast timeout */
   }
   timer = setTimeout(start_timer, timeout);
   // Call your code here.
};

$(document).ready(function() {
    start_timer();
});
于 2010-01-02T17:23:05.297 に答える
0

間隔自体は動的になるため、setTimeout代わりに使用する必要があります。

このようなもの(テストされていません):

$(document).ready(function() {
    $("#nownext").load('response.php?randval='+ Math.random());
    var minutes = new Date().getMinutes(), interval = 60*10*1000; // default 10 mins
    if (minutes <= 10 || (minutes > 30 && minutes < 35) || minutes > 50) {
        // update once-per-minute if within the first/last 10mins of the hour, or 5mins within the half hour
        interval = 60*1000;
    }
    setTimeout(arguments.callee, interval);
});
于 2010-01-02T17:11:28.800 に答える