Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

motto-timer

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

motto-timer - npm Package Compare versions

Comparing version
0.0.3
to
0.0.4
+24
-6
example/index.html
<!DOCTYPE html>
<html lang="en">
<head>

@@ -9,14 +10,31 @@ <meta charset="UTF-8">

</head>
<body>
<h1>example</h1>
<h1 id="container">倒计时60s</h1>
<button id="start">启动</button>
<button id="pause">暂停</button>
<button id="stop">停止</button>
<script src="../dist/bundle.js"></script>
<script>
(function (w) {
var timer = new w.Timer(1000, 2, function () {
console.log('test...');
(function () {
var startNode = document.getElementById('start'),
pauseNode = document.getElementById('pause'),
stopNode = document.getElementById('stop'),
containerNode = document.getElementById('container'),
timer = new Timer(1000, 60, function (params) {
containerNode.innerText = '倒计时' + (60 - params.cur_step) + 's';
});
startNode.addEventListener('click', function () {
timer.start();
});
timer.start();
})(window);
pauseNode.addEventListener('click', function () {
timer.pause();
});
stopNode.addEventListener('click', function () {
timer.stop();
});
})();
</script>
</body>
</html>
+1
-1
{
"name": "motto-timer",
"version": "0.0.3",
"version": "0.0.4",
"description": "简单易用的JS定时器",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -1,78 +0,78 @@

(function (w) {
/**
* 定时器构造函数
* @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;
}
/**
* 定时器
* @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;
}
/**
* 是否完成
* @returns {boolean} ture: 完成, false: 未完成
*/
Timer.prototype.isFinish = function () {
return this._step_size > 0 && this._step_size == this._cur_step;
};
/**
* 是否完成
* @return {boolean}
*/
Timer.prototype.isFinish = function () {
return this._step_size > 0 && this._step_size == this._cur_step;
};
/**
* 启动
*/
Timer.prototype.start = function () {
// this._cur_step = 0;
if (this._is_dead) return;
this._toggle = true;
var thiz = this;
setTimeout(function () { Timer.exec(thiz); }, this._interval);
};
/**
* 启动
*/
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.pause = function () {
this._toggle = false;
};
/**
* 暂停
*/
Timer.prototype.pause = function () {
this._toggle = false;
};
/**
* 停止
*/
Timer.prototype.stop = function () {
this._toggle = false;
this._is_dead = true;
this._step_size = this._cur_step = 1;
};
/**
* 停止
*/
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);
setTimeout(function () { Timer.exec(timer); }, timer._interval);
}
};
/**
* 执行定时器
* @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);
}
};
try {
window.Timer = Timer;
} catch (e) { }
w.Timer = Timer;
})(window);
module.exports = Timer;
var path = require('path');
module.exports = {
mode: 'production', // development or production
mode: 'development', // development or production
entry: './src/index.js',

@@ -6,0 +6,0 @@ output: {