ok,不废话了,实现一个javascript的Timer吧
比起as3的Timer类,功能上略有改动
timer2.src.js 复制代码 代码如下:
/**
* Timer 模型
*
* @author rainsilence
* @version 2.0
*/
(function() {
/**
* TimerEvent constructor 构造器
*
* @param type 事件类型
* @param bubbles 是否毛票
* @param cancelable 是否可取消
*/
TimerEvent = function(type, bubbles, cancelable) {
this.type = type;
this.bubbles = bubbles;
this.cancelable = cancelable;
};
/**
* Event 时间事件声明
*
* @event TIMER
* @event TIMER_COMPLETE
*/
extend(TimerEvent, {
TIMER : "timer",
TIMER_COMPLETE : "timerComplete"
});
/**
* Event 方法
*
* @method toString
*/
extend(TimerEvent.prototype, {
toString : function() {
return "[TimerEvent type=" + this.type +
" bubbles=" + this.bubbles +
" cancelable=" + this.cancelable +"]";
}
});
/**
* Extend 扩展类,对象的属性或者方法
*
* @param target 目标对象
* @param methods 这里改成param也许更合适,表示承载着对象,方法的对象,用于target的扩展
*/
function extend(target, methods) {
if (!target) {
target = {};
}
for (var prop in methods) {
target[prop] = methods[prop];
}
return target;
}
/**
* Timer 构造器
*
* @param delay 延时多少时间执行方法句柄
* @param repeatCount 重复多少次,如果不设置,代表重复无限次
*/
Timer = function(delay, repeatCount) {
var listenerMap = {};
listenerMap[TimerEvent.TIMER] = [];
listenerMap[TimerEvent.TIMER_COMPLETE] = [];
extend(this, {
currentCount : 0,
running : false,
delay : delay,
repeatCount : repeatCount,
// true:Interval,false:Timeout
repeatType : repeatCount == null || repeatCount < 1 ? true : false,
handler : listenerMap,
timerId : 0,
isCompleted : false
});
};
// 事件对象初始化(这部分未实现)
var timerEvent = new TimerEvent(TimerEvent.TIMER, false, false);
var timerCompleteEvent = new TimerEvent(TimerEvent.TIMER_COMPLETE, false, false);
/**
* Timer 计时器方法
*
* @method addEventListener 增加一个方法句柄(前两个参数必须,后一个参数可选)
* @method removeEventListener 移除一个方法句柄
* @method start 开始计时器
* @method stop 结束计时器
* @method reset 重置计时器
*/
extend(Timer.prototype, {
addEventListener : function(type, listener, useCapture) {
if (type == TimerEvent.TIMER || type == TimerEvent.TIMER_COMPLETE) {
if (!listener) {
alert("Listener is null");
}
if (useCapture == true) {
this.handler[type].splice(0, 0, [listener]);
} else {
this.handler[type].push(listener);
}
}
},
removeEventListener : function(type, listener) {
if (type == TimerEvent.TIMER || type == TimerEvent.TIMER_COMPLETE) {
if (!listener) {
this.handler[type] = [];
} else {
var listeners = this.handler[type];
for (var index = 0; index < listeners.length; index++) {
if (listeners[index] == listener) {
listeners.splice(index, 1);
break;
}
}
}
}
},
start : function() {
var timerThis = this;
if (this.running == true || this.isCompleted) {
return;
}
if (this.handler[TimerEvent.TIMER].length == 0 &&
this.handler[TimerEvent.TIMER_COMPLETE].length == 0) {
alert("No Function");
return;
}
if (this.repeatType) {
this.timerId = setInterval(function() {
dispachListener(timerThis.handler[TimerEvent.TIMER], timerEvent);
timerThis.currentCount++;
}, this.delay);
} else {
this.timerId = setTimeout(function() {delayExecute(timerThis.handler[TimerEvent.TIMER]);}, this.delay);
}
this.running = true;
function delayExecute(listeners) {
dispachListener(listeners, timerEvent);
timerThis.currentCount++;
if (timerThis.currentCount < timerThis.repeatCount) {
if (timerThis.running) {
timerThis.timerId = setTimeout(function() {delayExecute(listeners);}, timerThis.delay);
}
} else {
timerThis.running = false;
}
if (timerThis.running == false) {
if (!timerThis.isCompleted) {
dispachListener(timerThis.handler[TimerEvent.TIMER_COMPLETE], timerCompleteEvent);
}
timerThis.isCompleted = true;
}
}
function dispachListener(listeners, event) {
for (var prop in listeners) {
listeners[prop](event);
}
}
},
stop : function() {
this.running = false;
if (this.timerId == null) {
return;
}
if (this.repeatType) {
clearInterval(this.timerId);
} else {
clearTimeout(this.timerId);
}
if (!this.isCompleted) {
var listeners = this.handler[TimerEvent.TIMER_COMPLETE];
for (var prop in listeners) {
listeners[prop](timerCompleteEvent);
}
}
this.isCompleted = true;
},
reset : function() {
this.currentCount = 0;
this.isCompleted = false;
}
});
})();
接下来测试吧,大家见过新浪网上的滚动显示吗?用setTimeout写的,真叫牛叉。。。。。。换成Timer重构,简单易懂
timerTest.html 复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Insert title here</title>
<style type="text/css">
.rowLine {
width: 400px;
height: 80px;
border-bottom-style: solid;
border-width: 1px;
}
.barList {
border-style: solid;
border-width: 1px;
width:400px;
height: 80px;
overflow: hidden;
}
</style>
<script type="text/javascript" src="js/timer2.src.js"></script>
<script type="text/javascript">
<!--
var timer = new Timer(50);
var globalTimer = new Timer(10000);
var bList;
function init() {
bList = document.getElementById("barList");
timer.addEventListener(TimerEvent.TIMER, calTime);
timer.start();
globalTimer.addEventListener(TimerEvent.TIMER, controlTime);
globalTimer.start();
}
function controlTime() {
if (!timer.running) {
timer.reset();
timer.start();
}
}
function calTime() {
bList.scrollTop += 1;
if (bList.scrollTop > 80) {
timer.stop();
var barNode = bList.firstChild;
if (barNode.nodeType == 3) {
bList.appendChild(barNode);
bList.appendChild(bList.getElementsByTagName("div")[0]);
} else {
bList.appendChild(barNode);
}
bList.scrollTop = 0;
}
}
window.onload = init;
// -->
</script>
</head>
<body>
<div class="barList" id="barList">
<div class="rowLine" style="background-color: red" style="background-color: red">1</div>
<div class="rowLine" style="background-color: pink" style="background-color: pink">2</div>
<div class="rowLine" style="background-color: blue" style="background-color: blue">3</div>
<div class="rowLine" style="background-color: gray" style="background-color: gray">4</div>
</div>
</body>
</html>
addEventListener的useCapture参数本为捕获阶段触发之意,现在改成如果true,则在其他句柄之前触发,如果false,则在其他句柄之后触发。
后记:
现在貌似大家比较流行评论说明书的用法。。。比如struts+spring+hibernate。而忽略了编程的实质。希望大家多看源码,多讨论源码,那样才会有所谓的思想。否则人家今天用这个framework,明天换了。你又要从头开始了。
相关推荐:
AI免费文章生成器:轻松创作高质量内容的终极工具
怎么用AI润色文章,让你的文稿瞬间高大上
OpenAI您的信用卡被拒绝了?请尝试用借记卡支付,轻松解决支付问题!,ai cdr缩略图补丁
ChatGPT:如果您正在使用VPN,这些技巧您一定要知道!,AI换脸*H
今时CMS:引领数字化转型的智慧之选,河南seo优化网站联系方式
怎么用AI生成文章?全新写作方式的揭秘与应用指南
文章疑似AI生成怎么办?如何辨别并应对AI生成文章的挑战
ChatGPT无法加载?检查您的网络设置并尝试重启Chat,ai写作怎么写作文的
为什么seo这么难,seo难嘛 ,ai宁中则
SEO手法如何通过精准优化提升网站排名,获取海量流量,网站建设协议流程是什么
SEO优化需要花钱吗?从零起步,如何让SEO成为企业的“隐形财富”,字体如何往ai里面倒
ChatGPT的超链接点不开?解决方法一网打尽!,情感ai写作指令是什么
seo网站通过什么软件,网站seo软件哪个 ,战团ai
文章AI思维导图自动生成助力创作的智慧之源
怎么识别是AI写的文章
一秒采集:提升效率、创造价值的秘密武器,苏州网站整站优化
AI自动读文:让阅读更轻松、更高效的智能革命,寄ai空
zblog站群,zblog怎么样 ,欧卡2ai汽车如何使用
SEO优化大全:让你的网站排名轻松破局,精准引流更高效!,274357524ai
AI写作技巧,让创作事半功倍!
SEO总计:如何通过SEO优化实现网站流量和排名的飞跃,三元桥网站优化
seo追词是什么,seo词条 ,52580609AI
SEO优化的话题:助力企业成功的关键,夏杰ai智能管家
优化工具:提升工作效率的秘密武器,网站模板的优化策略是什么
ChatGPT桌面应用安装了,不能用?解决方案全攻略,让你轻松畅享AI助手!,绫波丽ai智能
SEO快速排名实例:如何通过实战技巧提升网站排名,快速突破流量瓶颈,安徽ai跑步机企业
优化公司:助力企业腾飞的秘密武器,微信营销推广价格多少
“标题制造机”:颠覆内容创作的秘密武器,助你轻松打造吸引力十足的标题,景区线上推广用哪些网站
AI仿写文章:开启内容创作新纪元
AI缩写文本:助力智能生活的革新力量,ai智能写作生成神器下载
seo营销方法是什么,seo营销模式 ,ai山海
互联网快照:记录数字时代的每个瞬间,全网seo怎么优化内容
WPJVX:开启数字化未来的智慧平台,关键词排名技术咨询乐云seo
什么是seo快排,seo快排方案 ,ai剪图形
ChatGPT维护页面-背后的技术与用户体验,ai领域ppt
seo黑帽是什么,列举几种seo黑帽行为 ,穿老款的ai丢人吗
优化网站的秘诀:提高网站速度与用户体验,助力业务腾飞,旅游网站建设步骤
AI提供的阅读书目对学生的专业知识有多大帮助,沃奇ai
怎么用AI写文:让创作更轻松,效率翻倍
优化平台:让数字化转型更简单、更高效,莆田谷歌seo品牌排行
SEO伪创:提升网站排名的危险策略与如何避免,怎样介绍社交网站推广
怎么用AI写出令人惊叹的文章?轻松搞定写作难题!
ChatGPT的VPN梯子:畅享全球互联网自由,打破地域限制,一直搜Ai
《*采集站:带你领略全球最全*资源的宝藏平台》,seo优化易下拉瞧瞧
交友群都有哪些,交友群是干什么的 ,ai猫csgo
SEO运营是什么职业,seo运营工程师招聘 ,把ai图层改横幅
从语言助手到智能生活伙伴,未来的智能助手如何改变我们的生活,网站建设分站公司
ChatGPT充值打不开?这几招教你轻松解决问题!,免费的ai写作绘图
Bing学术搜索结果不显示时间?如何解决这一问题,提升学术研究效率!,ai怎么参考线
ChatGDP人工智能:未来科技赋能企业与个人的智能变革,如何用AI绘制人体