motto-timer
Advanced tools
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <title>example</title> | ||
| </head> | ||
| <body> | ||
| <h1>example</h1> | ||
| <script src="../dist/bundle.js"></script> | ||
| <script> | ||
| (function (w) { | ||
| var timer = new w.Timer(1000, 2, function () { | ||
| console.log('test...'); | ||
| }); | ||
| timer.start(); | ||
| })(window); | ||
| </script> | ||
| </body> | ||
| </html> |
| var path = require('path'); | ||
| module.exports = { | ||
| mode: 'production', // development or production | ||
| entry: './src/index.js', | ||
| output: { | ||
| path: path.resolve(__dirname, 'dist'), | ||
| filename: 'bundle.js' | ||
| } | ||
| }; |
+11
-4
| { | ||
| "name": "motto-timer", | ||
| "version": "0.0.2", | ||
| "version": "0.0.3", | ||
| "description": "简单易用的JS定时器", | ||
| "main": "src/index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| "test": "echo \"Error: no test specified\" && exit 1", | ||
| "compile": "webpack", | ||
| "example": "http-server -p 8081" | ||
| }, | ||
@@ -17,4 +19,9 @@ "repository": { | ||
| ], | ||
| "author": "motto", | ||
| "license": "MIT" | ||
| "author": "https://gitee.com/motto", | ||
| "license": "MIT", | ||
| "devDependencies": { | ||
| "http-server": "^0.11.1", | ||
| "webpack": "^4.12.0", | ||
| "webpack-cli": "^3.0.6" | ||
| } | ||
| } |
+73
-68
@@ -1,73 +0,78 @@ | ||
| /** | ||
| * 定时器 | ||
| * @param {number} interval 间隔(单位:毫秒) | ||
| * @param {number} step_size 步长(小于或等于0表示无限循环,直到调用this.stop();才停止) | ||
| * @param {function} loop_fn 循环函数(执行时,会传入loop_fn_params,并增加cur_step属性) | ||
| * @param [可空] {JSON} loop_fn_params 循环函数参数 | ||
| * @param [可空] {function} finish_fn 任务结束函数(执行时,会传入finish_fn_params,并增加cur_step属性) | ||
| * @param [可空] {JSON} finish_fn_params 循环函数参数 | ||
| */ | ||
| function Timer(interval, step_size, loop_fn, loop_fn_params, finish_fn, finish_fn_params) { | ||
| this._interval = interval; | ||
| this._step_size = step_size; | ||
| this._loop_fn = $.type(loop_fn) != 'function' ? function () {} : loop_fn; | ||
| this._loop_fn_params = loop_fn_params == null ? {} : loop_fn_params; | ||
| this._finish_fn = $.type(finish_fn) != 'function' ? function () {} : finish_fn; | ||
| this._finish_fn_params = finish_fn_params == null ? {} : finish_fn_params; | ||
| this._toggle = false; | ||
| this._is_dead = false; | ||
| this._cur_step = 0; | ||
| } | ||
| (function (w) { | ||
| /** | ||
| * 是否完成 | ||
| * @return {boolean} | ||
| */ | ||
| Timer.prototype.isFinish = function () { | ||
| return this._step_size > 0 && this._step_size == this._cur_step; | ||
| }; | ||
| /** | ||
| * 定时器 | ||
| * @param {number} interval 间隔(单位:毫秒) | ||
| * @param {number} step_size 步长(小于或等于0表示无限循环,直到调用this.stop();才停止) | ||
| * @param {function} loop_fn 循环函数(执行时,会传入loop_fn_params,并增加cur_step属性) | ||
| * @param [可空] {JSON} loop_fn_params 循环函数参数 | ||
| * @param [可空] {function} finish_fn 任务结束函数(执行时,会传入finish_fn_params,并增加cur_step属性) | ||
| * @param [可空] {JSON} finish_fn_params 循环函数参数 | ||
| */ | ||
| function Timer(interval, step_size, loop_fn, loop_fn_params, finish_fn, finish_fn_params) { | ||
| this._interval = interval; | ||
| this._step_size = step_size; | ||
| this._loop_fn = typeof loop_fn !== 'function' ? function () { } : loop_fn; | ||
| this._loop_fn_params = loop_fn_params == null ? {} : loop_fn_params; | ||
| this._finish_fn = typeof finish_fn !== 'function' ? function () { } : finish_fn; | ||
| this._finish_fn_params = finish_fn_params == null ? {} : finish_fn_params; | ||
| this._toggle = false; | ||
| this._is_dead = false; | ||
| this._cur_step = 0; | ||
| } | ||
| /** | ||
| * 启动 | ||
| */ | ||
| Timer.prototype.start = function () { | ||
| // this._cur_step = 0; | ||
| if (this._is_dead) return ; | ||
| this._toggle = true; | ||
| var thiz = this; | ||
| window.setTimeout(function () {Timer.exec(thiz);}, this._interval); | ||
| }; | ||
| /** | ||
| * 是否完成 | ||
| * @return {boolean} | ||
| */ | ||
| Timer.prototype.isFinish = function () { | ||
| return this._step_size > 0 && this._step_size == this._cur_step; | ||
| }; | ||
| /** | ||
| * 暂停 | ||
| */ | ||
| Timer.prototype.pause = function () { | ||
| this._toggle = false; | ||
| }; | ||
| /** | ||
| * 启动 | ||
| */ | ||
| Timer.prototype.start = function () { | ||
| // this._cur_step = 0; | ||
| if (this._is_dead) return; | ||
| this._toggle = true; | ||
| var thiz = this; | ||
| window.setTimeout(function () { Timer.exec(thiz); }, this._interval); | ||
| }; | ||
| /** | ||
| * 停止 | ||
| */ | ||
| Timer.prototype.stop = function () { | ||
| this._toggle = false; | ||
| this._is_dead = true; | ||
| this._step_size = this._cur_step = 1; | ||
| }; | ||
| /** | ||
| * 暂停 | ||
| */ | ||
| Timer.prototype.pause = function () { | ||
| this._toggle = false; | ||
| }; | ||
| /** | ||
| * 执行定时器 | ||
| * @param {Timer} timer Timer对象 | ||
| */ | ||
| Timer.exec = function (timer) { | ||
| if (!timer._toggle) return ; | ||
| timer._cur_step ++; | ||
| if (timer.isFinish()) { | ||
| timer._finish_fn_params.cur_step = timer._cur_step; | ||
| timer._finish_fn(timer._finish_fn_params); | ||
| } | ||
| else { | ||
| timer._loop_fn_params['cur_step'] = timer._cur_step; | ||
| timer._loop_fn(timer._loop_fn_params); | ||
| window.setTimeout(function () {Timer.exec(timer);}, timer._interval); | ||
| } | ||
| }; | ||
| /** | ||
| * 停止 | ||
| */ | ||
| Timer.prototype.stop = function () { | ||
| this._toggle = false; | ||
| this._is_dead = true; | ||
| this._step_size = this._cur_step = 1; | ||
| }; | ||
| /** | ||
| * 执行定时器 | ||
| * @param {Timer} timer Timer对象 | ||
| */ | ||
| Timer.exec = function (timer) { | ||
| if (!timer._toggle) return; | ||
| timer._cur_step++; | ||
| if (timer.isFinish()) { | ||
| timer._finish_fn_params.cur_step = timer._cur_step; | ||
| timer._finish_fn(timer._finish_fn_params); | ||
| } else { | ||
| timer._loop_fn_params['cur_step'] = timer._cur_step; | ||
| timer._loop_fn(timer._loop_fn_params); | ||
| window.setTimeout(function () { Timer.exec(timer); }, timer._interval); | ||
| } | ||
| }; | ||
| w.Timer = Timer; | ||
| })(window); |
5881
30.78%6
50%77
14.93%3
Infinity%