很多时候需要在一段时间后执行多次功能.例如,您可能希望在给定时间后刷新屏幕. Prototype提供了一种使用 PeriodicalExecuter 对象实现它的简单机制.
PeriodicalExecuter 提供的优势在于它可以防止多个并行执行回调函数.
构造函数接受两个参数 :
回调函数.
执行之间的间隔(以秒为单位).
一旦启动,PeriodicalExecuter将无限期地触发,直到页面卸载或使用 stop()方法停止执行者.
以下示例将在每5秒钟后弹出一个对话框,直到你按"取消"按钮停止它.
<html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.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>