原型 - AJAX PeriodicalUpdater() 方法



此 AJAX 方法定期執行 AJAX 請求,並根據響應文字更新某個容器的內容。

容器透過提供 HTML 元素的 ID 來指定,如章節或段落。請參見以下示例。

回撥在請求的生命週期中各個點呼叫,且始終具有相同引數列表。它們與其他選項一起傳遞給請求者。

語法

new Ajax.PeriodicalUpdater(container, url[, options]);

Ajax.PeriodicalUpdater 具備 常見選項 和回撥,以及 Ajax.Updater(). 新增的選項和回撥。

該方法還有兩個特定選項 −

選項 描述
frequency

預設值為 2.

這是傳送 AJAX 請求的最短間隔。

decay

預設值為 1.

此項控制當響應沒有改變時請求間隔增長的比例。

返回值

返回 AJAX PeriodicalUpdater 物件。

停用和啟用 PeriodicalUpdater

只需呼叫其停止方法,便可停用正在執行的 PeriodicalUpdater。如果你希望稍後重新啟用它,只需呼叫其開始方法即可。兩者均不帶引數。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function startTimer() {
            new Ajax.PeriodicalUpdater('datetime', '/cgi-bin/timer.cgi', {
               method: 'get', frequency: 3, decay: 2
            });
         }
      </script>
   </head>

   <body>
      <p>Click start button to see how Current Time changes.</p>
      <p>This example may not work in IE.</p>
      <br />
 
      <div id = "datetime">Current Time</div>
      <br />
      <br />
      <input type = "button" value = "Start" onclick = "startTimer();"/>
   </body>
</html>

這是 timer.cgi 指令碼的內容 −

#!/usr/bin/perl

print "Content-type: text/html\n\n";

$datetime = localtime;
print $datetime;
print "<br />";

輸出

prototype_ajax_tutorial.htm
廣告