1 /* Timer 定时器
2
3 parameter:
4 func: Function; //定时器运行时的回调; 默认 null
5 speed: Number; //延迟多少毫秒执行一次 func; 默认 3000;
6 step: Integer; //执行多少次: 延迟speed毫秒执行一次 func; 默认 Infinity;
7 isStart: bool; //初始化时是否自动启动一次计时器(前提是 func 已被定义); 默认 true
8
9 attribute:
10 func, speed, step; //这些属性可以随时更改;
11
12 //只读属性
13 readyState: String; //定时器状态; 可能值: '', 'start', 'running', 'done'; ''表示定时器从未启动
14 number: Number; //运行的次数
15
16 method:
17 start(func, speed): this; //启动定时器 (如果定时器正在运行则什么都不会做)
18 restart(): undefined; //重启定时器
19 stop(): undefined; //停止定时器
20
21 demo:
22 //每 3000 毫秒 打印一次 timer.number, 重复 Infinity 次;
23 new Timer(timer => console.log(timer.number), 3000, Infinity);
24
25 */
26 class Timer{
27
28 #restart = -1;
29 #speed = 0;
30 #isRun = false;
31 #i = 0;
32 #readyState = ''; //start|running
33 //#paused = -1;
34
35 get number(){
36 return this.#i;
37 }
38
39 get readyState(){
40 return this.#i >= this.step ? 'done' : this.#readyState;
41 }
42
43 /* get paused(){
44 return this.#paused !== -1;
45 }
46
47 set paused(v){
48 if(v === true){
49
50 if(this.#i < this.step){
51 if(this.#paused === -1) this.#paused = this.#i;
52 this.stop();
53 }
54
55 }
56
57 else if(this.#paused !== -1){
58 this.restart();
59 this.#i = this.#paused;
60 this.#paused = -1;
61
62 }
63
64 } */
65
66 constructor(func = null, speed = 3000, step = 1, isStart = true){
67 this.func = func;
68 this.speed = speed;
69 this.step = step;
70 //this.onDone = null;
71
72 if(isStart === true && this.func !== null) this.restart();
73
74 }
75
76 copy(timer){
77 this.func = timer.func;
78 this.speed = timer.speed;
79 this.step = timer.step;
80 return this;
81 }
82
83 clone(){
84 return new this.constructor().copy(this);
85 }
86
87 start(func, time){
88 if(typeof func === 'function') this.func = func;
89 if(UTILS.isNumber(time) === true) this.speed = time;
90 this.restart();
91
92 return this;
93 }
94
95 restart(){
96 if(this.#isRun === false){
97 setTimeout(this._loop, this.speed);
98 this.#isRun = true;
99 this.#restart = -1;
100 this.#i = 0;
101 this.#readyState = 'start';
102
103 }
104
105 else{
106 this.#restart = Date.now();
107 this.#speed = this.speed;
108
109 }
110
111 }
112
113 stop(){
114 if(this.#isRun === true){
115 this.#restart = -1;
116 this.#i = this.step;
117 }
118
119 }
120
121 _loop = () => {
122
123 //重启计时器
124 if(this.#restart !== -1){
125
126 let gone = Date.now() - this.#restart;
127 this.#restart = -1;
128
129 if(gone >= this.#speed) gone = this.speed;
130 else{
131 if(this.#speed === this.speed) gone = this.#speed - gone;
132 else gone = (this.#speed - gone) / this.#speed * this.speed;
133 }
134
135 setTimeout(this._loop, gone);
136
137 this.#i = 1;
138 if(this.func !== null) this.func(this);
139
140 }
141
142 //正在运行
143 else if(this.#i < this.step){
144
145 setTimeout(this._loop, this.speed);
146
147 this.#i++;
148 if(this.#readyState !== 'running') this.#readyState = 'running';
149 if(this.func !== null) this.func(this);
150
151 }
152
153 //完成
154 else this.#isRun = false;
155
156 }
157
158 }

完整代码

parameter:
    func: Function; //定时器运行时的回调; 默认 null
    speed: Number; //延迟多少毫秒执行一次 func; 默认 3000;
    step: Integer; //执行多少次: 延迟speed毫秒执行一次 func; 默认 Infinity;
    isStart: bool; //初始化时是否自动启动一次计时器(前提是 func 已被定义); 默认 true
attribute:
    func, speed, step;  //这些属性可以随时更改;
    //只读属性
    readyState: String; //定时器状态; 可能值: '', 'start', 'running', 'done'; ''表示定时器从未启动
    number: Number;     //运行的次数
method:
    start(func, speed): this;   //启动定时器 (如果定时器正在运行则什么都不会做)
    restart(): undefined;       //重启定时器
    stop(): undefined;          //停止定时器
demo:
    //每 3000 毫秒 打印一次 timer.number, 重复 Infinity 次;
    new Timer(timer => console.log(timer.number), 3000, Infinity);
 
 

js 定时器 Timer的更多相关文章

  1. PHP框架Swoole的一个定时器Timer特性

    在各种业务型系统中,往往需要服务器在后台扫描相关数据,触发相应的统计.通知等操作. 比如对于一个项目管理系统,需要每天的特定时间内,统计每项任务的执行.到期情况.整个项目的进度等等,根据统计情况,做相 ...

  2. js定时器 特定时间执行某段程序的例子

    定时器想必大家并不陌生吧,在本文为大家详细介绍下js中是如何实现定时器的,具体原理及代码如下. 例子: $(function(){ var handler = function(){ //www.jb ...

  3. C#-WebForm JS定时器

    JS定时器: 1.window.setTimeout(function(){},3000) 延迟3秒执行 2.window.setInterval(function(){},3000) 也叫重复器,每 ...

  4. js定时器 离开当前页面任然执行的问题

    今天在博客上看到有人问 js定时器-----离开当前页面原本匀速运动的div加速了,回到页面若干时间恢复匀速??? 他是js定时器控制一个盒子做旋转动画 离开页面后js还在执行 但是盒子这个dom却被 ...

  5. JS定时器的用法及示例

    JS定时器的用法及示例 目录 js 定时器的四个方法 示例一 示例二 示例三 js 定时器的四个方法 setInterval() :按照指定的周期(以毫秒计)来调用函数或计算表达式.方法会不停地调用函 ...

  6. js定时器及定时器叠加问题

    回武汉隔离的第二天打卡,武汉加油,逆战必胜!今天想和大家简单聊一下js定时器的问题. 1.setTimeout 延时器 在指定时间后执行一次,注意只会执行一次 当然有的时候我们想用延时器做出定时器的效 ...

  7. python中实现定时器Timer

    实现定时器最简单的办法是就是循环中间嵌time.sleep(seconds), 这里我就不赘述了 # encoding: UTF-8 import threading #Timer(定时器)是Thre ...

  8. js定时器的使用(实例讲解)

    在javascritp中,有两个关于定时器的专用函数,分别为: 1.倒计定时器:timename=setTimeout("function();",delaytime);2.循环定 ...

  9. 订餐系统之定时器Timer不定时

    经过几天漫长的问题分析.处理.测试.验证,定时器Timer终于定时了,于是开始了这篇文章,希望对还在纠结于“定时器Timer不定时”的同学有所帮助,现在的方案,在系统日志中会有警告,如果您有更好的方案 ...

随机推荐

  1. 世界各国 MCC 和 MNC 列表

    http://www.cnblogs.com/inteliot/archive/2012/08/22/2651666.html常见MCC:代码(MCC)    ISO 3166-1    国家202 ...

  2. 人机交互BS

    B/S结构用户界面设计       [实验编号] 10003809548j Web界面设计 [实验学时] 8学时 [实验环境] l  所需硬件环境为微机: l  所需软件环境为dreamweaver ...

  3. iframe引入微信公众号文章

    微信在文章页面设置了响应头""frame-ancestors 'self'"阻止了外部页面将其嵌套的行为,文章的图片也设置了防盗链的功能,这就导致了直接在iframe中引 ...

  4. script标签中defer和async的区别(稀土掘金学习)

    如果没有defer或async属性,浏览器会立即加载并执行相应的脚本.它不会等待后续加载的文档元素,读取到就会开始加载和执行,这样就阻塞了后续文档的加载. 下图可以直观的看出三者之间的区别: 其中蓝色 ...

  5. 利用 onnxruntime 库同时推理多个模型的效率研究

    1. 背景 需求:针对视频形式的数据输入,对每一帧图像,有多个神经网络模型需要进行推理并获得预测结果.如何让整个推理过程更加高效,尝试了几种不同的方案. 硬件:单显卡主机. 2. 方案 由于存在多个模 ...

  6. Eureka Server 的 Instance Status 一直显示主机名问题

    Eureka Server 的 Instance Status 一直显示主机名问题 注册中心启动后,访问 http://localhost:8761/ 后: 如何调整为具体所在的服务器 IP 呢? 解 ...

  7. ASP.NET Core的几种服务器类型[共6篇]

    作为ASP.NET CORE请求处理管道的"龙头"的服务器负责监听和接收请求并最终完成对请求的响应.它将原始的请求上下文描述为相应的特性(Feature),并以此将HttpCont ...

  8. 使用 HTML5 input 类型提升移动端输入体验(转翻译)

    在过去的几年里,在移动设备上浏览网页已变得难以置信的受欢迎. 但是这些设备上的浏览体验,有时遗留很多的有待改进.当涉及到填写表单时,这一点尤为明显.幸运的是,HTML5规范引入了许多新input类型, ...

  9. HCIE笔记-第六节-CIDR与ICMP

    项目部 58人 地址:194.2.3.128 /26 研发部 100人 地址: 194.2.3.0/25 市场部 27人 地址: 194.2.3.192/27 财务部 15人 地址:194.2.3.2 ...

  10. 『现学现忘』Git基础 — 12、Git用户签名(补充)

    目录 1.修改用户签名 2.取消用户签名 3.用户签名的优先级 4.总结本文用到的Git命令 1.修改用户签名 其实很简单,就是重新执行git config命令,换个用户名和邮箱地址就可以了,新配置的 ...