AJAX PeriodicalUpdater() 方法 | Prototype教程

返回到:Prototype AJAX 教程

此 AJAX 方法定期执行 AJAX 请求并根据响应文本更新容器的内容。

容器是通过给定 HTML 元素的 ID 来指定的,比如分区或段落。请参见下面的示例。

回调在请求生命周期的不同点被调用,并且始终具有相同的参数列表。它们与其他选项一起传递给请求者。

语法

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

Ajax.PeriodicalUpdater 具有所有常用选项和回调,以及由Ajax.Updater() 添加的那些。.

此方法还有两个特定选项 –

选项描述
frequency默认值为 2
这是发出 AJAX 请求的最小间隔。
decay默认值为 1
这控制了响应不变时请求间隔增长的速率。

返回值

返回 AJAX PeriodicalUpdater 对象。

禁用和启用 PeriodicalUpdater

您可以通过简单地调用其停止方法来停止正在运行的 PeriodicalUpdater。如果你想稍后重新启用它,只需调用它的 start 方法。两者都没有争论。

例子

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "https://cdn.bootcdn.net/ajax/libs/prototype/1.7.3/prototype.min.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 教程

作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8306.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年1月25日
下一篇 2023年1月25日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注