作家
登录

Prototype PeriodicalExecuter对象 学习

作者: 来源:www.28hudong.com 2013-03-30 02:28:45 阅读 我要评论

This is a simple facility for periodical execution of a function. This essentially encapsulates the native clearInterval/setInterval mechanism found in native Window objects. This is especially useful if you use one to interact with the user at given intervals (e.g. use a prompt or confirm call): this will avoid multiple message boxes all waiting to be actioned. 这个对象就是可以周期性的执行某个方法,但是在它内部维持了一个状态,可以防止由于某些原因一次调用没执行,然后下一次调用又来了,这样会造成连续执行两次方法。上面的第二断英文就是这个意思。 帮助文档上说这个对象只提供了一个方法stop,但是在我看的源码里还提供了一个事件onTimerEvent,应该可以在某个时候触发这个事件。但帮助文档上没有给出示例。 这个对象源码比较简单,这里直接贴出来了,就不再注释了: 复制代码 代码如下:var PeriodicalExecuter = Class.create({ initialize: function(callback, frequency) { this.callback = callback; this.frequency = frequency; this.currentlyExecuting = false; this.registerCallback(); }, registerCallback: function() { this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); }, execute: function() { this.callback(this); }, stop: function() { if (!this.timer) return; clearInterval(this.timer); this.timer = null; }, onTimerEvent: function() { if (!this.currentlyExecuting) { try { this.currentlyExecuting = true; this.execute(); } catch(e) { /* empty catch for clients that don't support try/finally */ } finally { this.currentlyExecuting = false; } } } });看一下示例: 复制代码 代码如下:new PeriodicalExecuter(function(pe) { if (!confirm('Want me to annoy you again later?')) pe.stop(); }, 5); // Note that there won't be a stack of such messages if the user takes too long // answering to the question...

  推荐阅读

  DOM 基本方法

直接引用结点 1.document.getElementById(id); --在文档里面通过id来找结点 2.document.getElementByTagName(tagName); --返回一个数组,包含对这些结点的引用 --如:document.getElementByTagName("span");将返回>>>详细阅读


本文标题:Prototype PeriodicalExecuter对象 学习

地址:http://www.17bianji.com/kaifa2/JS/28428.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)