Categories: Prototype教程

Prototype – 定期执行

很多时候需要在一定时间后多次执行一个函数。例如,您可能希望在给定时间后刷新屏幕。Prototype 提供了一种简单的机制来使用PeriodicalExecuter对象来实现它。

PeriodicalExecuter提供的优势在于它可以保护您免受回调函数的多次并行执行。

创建一个 PeriodicalExecuter

构造函数有两个参数:

  • 回调函数。
  • 执行之间的间隔(以秒为单位)。

一旦启动,PeriodicalExecuter 将无限期触发,直到页面卸载或使用stop()方法停止执行程序。

例子

以下是每 5 秒后弹出一个对话框的示例,直到您按“取消”按钮停止它。

<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 startExec() {
            new PeriodicalExecuter(function(pe) {
               if (!confirm('Want me to annoy you again later?'))
               pe.stop();
            }, 5);
         }
      </script>
   </head>

   <body>
      <p>Click start button to start periodic executer:</p>
      <br />
      <br />
      <input type = "button" value = "start" onclick = "startExec();"/>
   </body>
</html>

terry

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

1 天 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

1 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

2 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

3 周 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

3 周 ago

聊聊React和Vue组件更新的实现及区别

React 和 Vue 都是当…

4 周 ago