🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

countdown-pro

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

countdown-pro - npm Package Compare versions

Comparing version
1.0.2
to
2.0.0
+165
dist/countdown.cjs.js
'use strict';
/**
* 前置补零,如果值的长度小于目标长度,则前置补零,否则不处理
*
* @param num 待处理的值
* @param targetLength 目标长度
* @returns 补零后的值
*/
function padZero(num, targetLength) {
if (targetLength === void 0) { targetLength = 2; }
var str = num + '';
while (str.length < targetLength) {
str = '0' + str;
}
return str;
}
var SECOND = 1000;
var MINUTE = 60 * SECOND;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
/**
* 解析时间戳
*
* @param {number} timestamp 时间戳,单位毫秒
* @returns 包含日/时/分/秒/毫秒的对象
*/
function parseTimeData(timestamp) {
return {
days: Math.floor(timestamp / DAY),
hours: Math.floor((timestamp % DAY) / HOUR),
minutes: Math.floor((timestamp % HOUR) / MINUTE),
seconds: Math.floor((timestamp % MINUTE) / SECOND),
milliseconds: Math.floor(timestamp % SECOND)
};
}
/**
* 格式化时间格式
*
* @param format 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒
* @param timeData 包含日/时/分/秒/毫秒的对象
* @returns 返回格式化后的时间字符串
*/
function parseFormat(format, timeData) {
// eslint-disable-next-line prefer-const
var days = timeData.days, hours = timeData.hours, minutes = timeData.minutes, seconds = timeData.seconds, milliseconds = timeData.milliseconds;
if (format.indexOf('DD') === -1) {
hours += days * 24;
}
else {
format = format.replace('DD', padZero(days));
}
if (format.indexOf('HH') === -1) {
minutes += hours * 60;
}
else {
format = format.replace('HH', padZero(hours));
}
if (format.indexOf('mm') === -1) {
seconds += minutes * 60;
}
else {
format = format.replace('mm', padZero(minutes));
}
if (format.indexOf('ss') === -1) {
milliseconds += seconds * 1000;
}
else {
format = format.replace('ss', padZero(seconds));
}
return format.replace('SSS', padZero(milliseconds, 3));
}
/**
* 格式化时间
*
* @param {number} timestamp 时间戳,单位毫秒
* @param {string} [pattern='HH:mm:ss'] 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒。默认值为 HH:mm:ss
* @returns {string} 返回格式化后的时间字符串
*/
function format(timestamp, pattern) {
if (pattern === void 0) { pattern = 'HH:mm:ss'; }
var timeData = parseTimeData(timestamp);
return parseFormat(pattern, timeData);
}
var CountDown = /** @class */ (function () {
function CountDown(options) {
this.options = {
time: 0,
interval: 1000
};
for (var prop in options) {
if (Object.prototype.hasOwnProperty.call(options, prop)) {
// @ts-ignore
this.options[prop] = options[prop];
}
}
this.options.time =
typeof this.options.time !== 'number' || this.options.time < 0
? 0
: Math.ceil(this.options.time); // 倒计时长
this.timer = null; // 定时器
this.counting = false; // 标识正在倒计时
this.completed = false; // 标识倒计时完成
this.currentTime = this.options.time; // 记录当前倒计时长
}
// 开始
CountDown.prototype.start = function () {
if (this.counting) {
return;
}
this.counting = true;
this.tick();
};
// 暂停
CountDown.prototype.pause = function () {
clearTimeout(this.timer);
this.counting = false;
};
// 重置
CountDown.prototype.reset = function () {
this.pause();
this.completed = false;
this.currentTime = this.options.time;
this.handleChange();
};
CountDown.prototype.handleChange = function () {
var _a, _b;
(_b = (_a = this.options).onChange) === null || _b === void 0 ? void 0 : _b.call(_a, this.currentTime);
};
CountDown.prototype.handleEnd = function () {
var _a, _b;
this.pause();
this.completed = true;
(_b = (_a = this.options).onEnd) === null || _b === void 0 ? void 0 : _b.call(_a);
};
CountDown.prototype.tick = function () {
var that = this;
var interval = that.options.interval;
if (that.completed) {
that.handleEnd();
return;
}
that.timer = setTimeout(function () {
that.currentTime -= interval;
if (that.currentTime < 0) {
that.currentTime = 0;
}
that.handleChange();
if (that.currentTime <= 0) {
that.handleEnd();
}
else {
that.tick();
}
}, interval);
};
CountDown.format = format;
CountDown.parseTimeData = parseTimeData;
CountDown.parseFormat = parseFormat;
CountDown.padZero = padZero;
return CountDown;
}());
module.exports = CountDown;
import { format, parseTimeData, parseFormat, padZero } from './util';
declare type Options = {
time: number;
interval?: number;
onChange?: (currentTime: number) => void;
onEnd?: () => void;
};
declare class CountDown {
options: Options & {
time: number;
interval: number;
};
private timer;
private counting;
private completed;
private currentTime;
constructor(options: Options);
start(): void;
pause(): void;
reset(): void;
private handleChange;
private handleEnd;
private tick;
static format: typeof format;
static parseTimeData: typeof parseTimeData;
static parseFormat: typeof parseFormat;
static padZero: typeof padZero;
}
export default CountDown;
/**
* 前置补零,如果值的长度小于目标长度,则前置补零,否则不处理
*
* @param num 待处理的值
* @param targetLength 目标长度
* @returns 补零后的值
*/
function padZero(num, targetLength) {
if (targetLength === void 0) { targetLength = 2; }
var str = num + '';
while (str.length < targetLength) {
str = '0' + str;
}
return str;
}
var SECOND = 1000;
var MINUTE = 60 * SECOND;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
/**
* 解析时间戳
*
* @param {number} timestamp 时间戳,单位毫秒
* @returns 包含日/时/分/秒/毫秒的对象
*/
function parseTimeData(timestamp) {
return {
days: Math.floor(timestamp / DAY),
hours: Math.floor((timestamp % DAY) / HOUR),
minutes: Math.floor((timestamp % HOUR) / MINUTE),
seconds: Math.floor((timestamp % MINUTE) / SECOND),
milliseconds: Math.floor(timestamp % SECOND)
};
}
/**
* 格式化时间格式
*
* @param format 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒
* @param timeData 包含日/时/分/秒/毫秒的对象
* @returns 返回格式化后的时间字符串
*/
function parseFormat(format, timeData) {
// eslint-disable-next-line prefer-const
var days = timeData.days, hours = timeData.hours, minutes = timeData.minutes, seconds = timeData.seconds, milliseconds = timeData.milliseconds;
if (format.indexOf('DD') === -1) {
hours += days * 24;
}
else {
format = format.replace('DD', padZero(days));
}
if (format.indexOf('HH') === -1) {
minutes += hours * 60;
}
else {
format = format.replace('HH', padZero(hours));
}
if (format.indexOf('mm') === -1) {
seconds += minutes * 60;
}
else {
format = format.replace('mm', padZero(minutes));
}
if (format.indexOf('ss') === -1) {
milliseconds += seconds * 1000;
}
else {
format = format.replace('ss', padZero(seconds));
}
return format.replace('SSS', padZero(milliseconds, 3));
}
/**
* 格式化时间
*
* @param {number} timestamp 时间戳,单位毫秒
* @param {string} [pattern='HH:mm:ss'] 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒。默认值为 HH:mm:ss
* @returns {string} 返回格式化后的时间字符串
*/
function format(timestamp, pattern) {
if (pattern === void 0) { pattern = 'HH:mm:ss'; }
var timeData = parseTimeData(timestamp);
return parseFormat(pattern, timeData);
}
var CountDown = /** @class */ (function () {
function CountDown(options) {
this.options = {
time: 0,
interval: 1000
};
for (var prop in options) {
if (Object.prototype.hasOwnProperty.call(options, prop)) {
// @ts-ignore
this.options[prop] = options[prop];
}
}
this.options.time =
typeof this.options.time !== 'number' || this.options.time < 0
? 0
: Math.ceil(this.options.time); // 倒计时长
this.timer = null; // 定时器
this.counting = false; // 标识正在倒计时
this.completed = false; // 标识倒计时完成
this.currentTime = this.options.time; // 记录当前倒计时长
}
// 开始
CountDown.prototype.start = function () {
if (this.counting) {
return;
}
this.counting = true;
this.tick();
};
// 暂停
CountDown.prototype.pause = function () {
clearTimeout(this.timer);
this.counting = false;
};
// 重置
CountDown.prototype.reset = function () {
this.pause();
this.completed = false;
this.currentTime = this.options.time;
this.handleChange();
};
CountDown.prototype.handleChange = function () {
var _a, _b;
(_b = (_a = this.options).onChange) === null || _b === void 0 ? void 0 : _b.call(_a, this.currentTime);
};
CountDown.prototype.handleEnd = function () {
var _a, _b;
this.pause();
this.completed = true;
(_b = (_a = this.options).onEnd) === null || _b === void 0 ? void 0 : _b.call(_a);
};
CountDown.prototype.tick = function () {
var that = this;
var interval = that.options.interval;
if (that.completed) {
that.handleEnd();
return;
}
that.timer = setTimeout(function () {
that.currentTime -= interval;
if (that.currentTime < 0) {
that.currentTime = 0;
}
that.handleChange();
if (that.currentTime <= 0) {
that.handleEnd();
}
else {
that.tick();
}
}, interval);
};
CountDown.format = format;
CountDown.parseTimeData = parseTimeData;
CountDown.parseFormat = parseFormat;
CountDown.padZero = padZero;
return CountDown;
}());
export { CountDown as default };
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.CountDown = factory());
})(this, (function () { 'use strict';
/**
* 前置补零,如果值的长度小于目标长度,则前置补零,否则不处理
*
* @param num 待处理的值
* @param targetLength 目标长度
* @returns 补零后的值
*/
function padZero(num, targetLength) {
if (targetLength === void 0) { targetLength = 2; }
var str = num + '';
while (str.length < targetLength) {
str = '0' + str;
}
return str;
}
var SECOND = 1000;
var MINUTE = 60 * SECOND;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
/**
* 解析时间戳
*
* @param {number} timestamp 时间戳,单位毫秒
* @returns 包含日/时/分/秒/毫秒的对象
*/
function parseTimeData(timestamp) {
return {
days: Math.floor(timestamp / DAY),
hours: Math.floor((timestamp % DAY) / HOUR),
minutes: Math.floor((timestamp % HOUR) / MINUTE),
seconds: Math.floor((timestamp % MINUTE) / SECOND),
milliseconds: Math.floor(timestamp % SECOND)
};
}
/**
* 格式化时间格式
*
* @param format 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒
* @param timeData 包含日/时/分/秒/毫秒的对象
* @returns 返回格式化后的时间字符串
*/
function parseFormat(format, timeData) {
// eslint-disable-next-line prefer-const
var days = timeData.days, hours = timeData.hours, minutes = timeData.minutes, seconds = timeData.seconds, milliseconds = timeData.milliseconds;
if (format.indexOf('DD') === -1) {
hours += days * 24;
}
else {
format = format.replace('DD', padZero(days));
}
if (format.indexOf('HH') === -1) {
minutes += hours * 60;
}
else {
format = format.replace('HH', padZero(hours));
}
if (format.indexOf('mm') === -1) {
seconds += minutes * 60;
}
else {
format = format.replace('mm', padZero(minutes));
}
if (format.indexOf('ss') === -1) {
milliseconds += seconds * 1000;
}
else {
format = format.replace('ss', padZero(seconds));
}
return format.replace('SSS', padZero(milliseconds, 3));
}
/**
* 格式化时间
*
* @param {number} timestamp 时间戳,单位毫秒
* @param {string} [pattern='HH:mm:ss'] 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒。默认值为 HH:mm:ss
* @returns {string} 返回格式化后的时间字符串
*/
function format(timestamp, pattern) {
if (pattern === void 0) { pattern = 'HH:mm:ss'; }
var timeData = parseTimeData(timestamp);
return parseFormat(pattern, timeData);
}
var CountDown = /** @class */ (function () {
function CountDown(options) {
this.options = {
time: 0,
interval: 1000
};
for (var prop in options) {
if (Object.prototype.hasOwnProperty.call(options, prop)) {
// @ts-ignore
this.options[prop] = options[prop];
}
}
this.options.time =
typeof this.options.time !== 'number' || this.options.time < 0
? 0
: Math.ceil(this.options.time); // 倒计时长
this.timer = null; // 定时器
this.counting = false; // 标识正在倒计时
this.completed = false; // 标识倒计时完成
this.currentTime = this.options.time; // 记录当前倒计时长
}
// 开始
CountDown.prototype.start = function () {
if (this.counting) {
return;
}
this.counting = true;
this.tick();
};
// 暂停
CountDown.prototype.pause = function () {
clearTimeout(this.timer);
this.counting = false;
};
// 重置
CountDown.prototype.reset = function () {
this.pause();
this.completed = false;
this.currentTime = this.options.time;
this.handleChange();
};
CountDown.prototype.handleChange = function () {
var _a, _b;
(_b = (_a = this.options).onChange) === null || _b === void 0 ? void 0 : _b.call(_a, this.currentTime);
};
CountDown.prototype.handleEnd = function () {
var _a, _b;
this.pause();
this.completed = true;
(_b = (_a = this.options).onEnd) === null || _b === void 0 ? void 0 : _b.call(_a);
};
CountDown.prototype.tick = function () {
var that = this;
var interval = that.options.interval;
if (that.completed) {
that.handleEnd();
return;
}
that.timer = setTimeout(function () {
that.currentTime -= interval;
if (that.currentTime < 0) {
that.currentTime = 0;
}
that.handleChange();
if (that.currentTime <= 0) {
that.handleEnd();
}
else {
that.tick();
}
}, interval);
};
CountDown.format = format;
CountDown.parseTimeData = parseTimeData;
CountDown.parseFormat = parseFormat;
CountDown.padZero = padZero;
return CountDown;
}());
return CountDown;
}));
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).CountDown=e()}(this,(function(){"use strict";function t(t,e){void 0===e&&(e=2);for(var i=t+"";i.length<e;)i="0"+i;return i}var e=1e3,i=6e4,n=36e5,o=24*n;function s(t){return{days:Math.floor(t/o),hours:Math.floor(t%o/n),minutes:Math.floor(t%n/i),seconds:Math.floor(t%i/e),milliseconds:Math.floor(t%e)}}function r(e,i){var n=i.days,o=i.hours,s=i.minutes,r=i.seconds,u=i.milliseconds;return-1===e.indexOf("DD")?o+=24*n:e=e.replace("DD",t(n)),-1===e.indexOf("HH")?s+=60*o:e=e.replace("HH",t(o)),-1===e.indexOf("mm")?r+=60*s:e=e.replace("mm",t(s)),-1===e.indexOf("ss")?u+=1e3*r:e=e.replace("ss",t(r)),e.replace("SSS",t(u,3))}function u(t,e){return void 0===e&&(e="HH:mm:ss"),r(e,s(t))}return function(){function e(t){for(var e in this.options={time:0,interval:1e3},t)Object.prototype.hasOwnProperty.call(t,e)&&(this.options[e]=t[e]);this.options.time="number"!=typeof this.options.time||this.options.time<0?0:Math.ceil(this.options.time),this.timer=null,this.counting=!1,this.completed=!1,this.currentTime=this.options.time}return e.prototype.start=function(){this.counting||(this.counting=!0,this.tick())},e.prototype.pause=function(){clearTimeout(this.timer),this.counting=!1},e.prototype.reset=function(){this.pause(),this.completed=!1,this.currentTime=this.options.time,this.handleChange()},e.prototype.handleChange=function(){var t,e;null===(e=(t=this.options).onChange)||void 0===e||e.call(t,this.currentTime)},e.prototype.handleEnd=function(){var t,e;this.pause(),this.completed=!0,null===(e=(t=this.options).onEnd)||void 0===e||e.call(t)},e.prototype.tick=function(){var t=this,e=t.options.interval;t.completed?t.handleEnd():t.timer=setTimeout((function(){t.currentTime-=e,t.currentTime<0&&(t.currentTime=0),t.handleChange(),t.currentTime<=0?t.handleEnd():t.tick()}),e)},e.format=u,e.parseTimeData=s,e.parseFormat=r,e.padZero=t,e}()}));
//# sourceMappingURL=countdown.umd.min.js.map
{"version":3,"file":"countdown.umd.min.js","sources":["../src/util.ts","../src/countdown.ts"],"sourcesContent":["/**\n * 前置补零,如果值的长度小于目标长度,则前置补零,否则不处理\n *\n * @param num 待处理的值\n * @param targetLength 目标长度\n * @returns 补零后的值\n */\nexport function padZero(num: string | number, targetLength = 2) {\n let str = num + '';\n while (str.length < targetLength) {\n str = '0' + str;\n }\n return str;\n}\n\nconst SECOND = 1000;\nconst MINUTE = 60 * SECOND;\nconst HOUR = 60 * MINUTE;\nconst DAY = 24 * HOUR;\n\n/**\n * 解析时间戳\n *\n * @param {number} timestamp 时间戳,单位毫秒\n * @returns 包含日/时/分/秒/毫秒的对象\n */\nexport function parseTimeData(timestamp: number) {\n return {\n days: Math.floor(timestamp / DAY),\n hours: Math.floor((timestamp % DAY) / HOUR),\n minutes: Math.floor((timestamp % HOUR) / MINUTE),\n seconds: Math.floor((timestamp % MINUTE) / SECOND),\n milliseconds: Math.floor(timestamp % SECOND)\n };\n}\n\n/**\n * 格式化时间格式\n *\n * @param format 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒\n * @param timeData 包含日/时/分/秒/毫秒的对象\n * @returns 返回格式化后的时间字符串\n */\nexport function parseFormat(format: string, timeData: ReturnType<typeof parseTimeData>) {\n // eslint-disable-next-line prefer-const\n let { days, hours, minutes, seconds, milliseconds } = timeData;\n\n if (format.indexOf('DD') === -1) {\n hours += days * 24;\n } else {\n format = format.replace('DD', padZero(days));\n }\n\n if (format.indexOf('HH') === -1) {\n minutes += hours * 60;\n } else {\n format = format.replace('HH', padZero(hours));\n }\n\n if (format.indexOf('mm') === -1) {\n seconds += minutes * 60;\n } else {\n format = format.replace('mm', padZero(minutes));\n }\n\n if (format.indexOf('ss') === -1) {\n milliseconds += seconds * 1000;\n } else {\n format = format.replace('ss', padZero(seconds));\n }\n\n return format.replace('SSS', padZero(milliseconds, 3));\n}\n\n/**\n * 格式化时间\n *\n * @param {number} timestamp 时间戳,单位毫秒\n * @param {string} [pattern='HH:mm:ss'] 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒。默认值为 HH:mm:ss\n * @returns {string} 返回格式化后的时间字符串\n */\nexport function format(timestamp: number, pattern = 'HH:mm:ss') {\n const timeData = parseTimeData(timestamp);\n return parseFormat(pattern, timeData);\n}\n","import { format, parseTimeData, parseFormat, padZero } from './util';\n\ntype Options = {\n time: number;\n interval?: number;\n onChange?: (currentTime: number) => void;\n onEnd?: () => void;\n};\n\nclass CountDown {\n options: Options & { time: number; interval: number };\n\n private timer: any;\n private counting: boolean;\n private completed: boolean;\n private currentTime: number;\n\n constructor(options: Options) {\n this.options = {\n time: 0,\n interval: 1000\n };\n\n for (const prop in options) {\n if (Object.prototype.hasOwnProperty.call(options, prop)) {\n // @ts-ignore\n this.options[prop] = options[prop];\n }\n }\n\n this.options.time =\n typeof this.options.time !== 'number' || this.options.time < 0\n ? 0\n : Math.ceil(this.options.time); // 倒计时长\n\n this.timer = null; // 定时器\n this.counting = false; // 标识正在倒计时\n this.completed = false; // 标识倒计时完成\n\n this.currentTime = this.options.time; // 记录当前倒计时长\n }\n\n // 开始\n start() {\n if (this.counting) {\n return;\n }\n\n this.counting = true;\n this.tick();\n }\n\n // 暂停\n pause() {\n clearTimeout(this.timer);\n this.counting = false;\n }\n\n // 重置\n reset() {\n this.pause();\n this.completed = false;\n this.currentTime = this.options.time;\n this.handleChange();\n }\n\n private handleChange() {\n this.options.onChange?.(this.currentTime);\n }\n\n private handleEnd() {\n this.pause();\n this.completed = true;\n this.options.onEnd?.();\n }\n\n private tick() {\n const that = this;\n const interval = that.options.interval;\n\n if (that.completed) {\n that.handleEnd();\n return;\n }\n\n that.timer = setTimeout(function () {\n that.currentTime -= interval;\n\n if (that.currentTime < 0) {\n that.currentTime = 0;\n }\n\n that.handleChange();\n\n if (that.currentTime <= 0) {\n that.handleEnd();\n } else {\n that.tick();\n }\n }, interval);\n }\n\n static format = format;\n static parseTimeData = parseTimeData;\n static parseFormat = parseFormat;\n static padZero = padZero;\n}\n\nexport default CountDown;\n"],"names":["padZero","num","targetLength","str","length","SECOND","MINUTE","HOUR","DAY","parseTimeData","timestamp","days","Math","floor","hours","minutes","seconds","milliseconds","parseFormat","format","timeData","indexOf","replace","pattern","CountDown","options","prop","this","time","interval","Object","prototype","hasOwnProperty","call","ceil","timer","counting","completed","currentTime","start","tick","pause","clearTimeout","reset","handleChange","_b","_a","onChange","handleEnd","onEnd","that","setTimeout"],"mappings":"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"}
/**
* 前置补零,如果值的长度小于目标长度,则前置补零,否则不处理
*
* @param num 待处理的值
* @param targetLength 目标长度
* @returns 补零后的值
*/
export declare function padZero(num: string | number, targetLength?: number): string;
/**
* 解析时间戳
*
* @param {number} timestamp 时间戳,单位毫秒
* @returns 包含日/时/分/秒/毫秒的对象
*/
export declare function parseTimeData(timestamp: number): {
days: number;
hours: number;
minutes: number;
seconds: number;
milliseconds: number;
};
/**
* 格式化时间格式
*
* @param format 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒
* @param timeData 包含日/时/分/秒/毫秒的对象
* @returns 返回格式化后的时间字符串
*/
export declare function parseFormat(format: string, timeData: ReturnType<typeof parseTimeData>): string;
/**
* 格式化时间
*
* @param {number} timestamp 时间戳,单位毫秒
* @param {string} [pattern='HH:mm:ss'] 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒。默认值为 HH:mm:ss
* @returns {string} 返回格式化后的时间字符串
*/
export declare function format(timestamp: number, pattern?: string): string;
+50
-11
{
"name": "countdown-pro",
"version": "1.0.2",
"version": "2.0.0",
"description": "A simple countdown.",
"main": "lib/countdown.js",
"types": "types/index.d.ts",
"main": "dist/countdown.cjs.js",
"module": "dist/countdown.esm.js",
"types": "dist/countdown.d.ts",
"files": [
"dist"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "npx gulp && cp types/util.d.ts lib"
"test": "jest --verbose",
"build": "rm -rf dist && rollup -c",
"lint": "eslint --ext .js,.jsx,.ts,.tsx src",
"lint-fix": "npm run lint -- --fix",
"prettier": "prettier --write **/*",
"commit": "cz",
"prepublishOnly": "npm test && npm run build"
},
"lint-staged": {
"**/*.{js,jsx,ts,tsx}": "eslint",
"**/*.{css,scss,less,js,jsx,ts,tsx,json,md}": "prettier -w"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"gitHooks": {
"pre-commit": "lint-staged",
"commit-msg": "npx --no -- commitlint --edit \"$1\""
},
"repository": {

@@ -25,9 +47,26 @@ "type": "git",

"devDependencies": {
"gulp": "^4.0.0",
"gulp-cli": "^2.2.0",
"gulp-concat": "^2.6.1",
"gulp-rename": "^1.2.2",
"gulp-uglify": "^3.0.0",
"pump": "^1.0.2"
"@babel/core": "^7.19.0",
"@babel/preset-env": "^7.19.0",
"@babel/preset-typescript": "^7.18.6",
"@commitlint/cli": "^17.1.2",
"@commitlint/config-conventional": "^17.1.0",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-node-resolve": "^14.0.1",
"@rollup/plugin-typescript": "^8.5.0",
"@types/jest": "^29.0.0",
"@typescript-eslint/eslint-plugin": "^5.36.2",
"@typescript-eslint/parser": "^5.36.2",
"babel-jest": "^29.0.2",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.23.0",
"jest": "^29.0.2",
"lint-staged": "^13.0.3",
"prettier": "^2.7.1",
"rollup": "^2.79.0",
"rollup-plugin-terser": "^7.0.2",
"tslib": "^2.4.0",
"typescript": "^4.8.3",
"yorkie": "^2.0.0"
}
}
+36
-55
# CountDown
[![npm][npm]][npm-url] ![npm](https://img.shields.io/npm/dt/countdown-pro)
一个简单的倒计时。

@@ -23,4 +25,4 @@

- 该仓库的 [dist](https://github.com/caijf/countdown/tree/master/dist) 目录下提供了 `countdown.js` 以及 `countdown.min.js`。
- `npm` 包的 `countdown-pro/dist` 目录下也提供了 `countdown.js` 以及 `countdown.min.js`。
- 该仓库的 [dist](https://github.com/caijf/countdown/tree/master/dist) 目录下提供了 `countdown.umd.js` 以及 `countdown.umd.min.js`。
- `npm` 包的 `countdown-pro/dist` 目录下也提供了 `countdown.umd.js` 以及 `countdown.umd.min.js`。
- 你也可以通过 [UNPKG](https://unpkg.com/countdown-pro@latest/dist/) 进行下载。

@@ -30,3 +32,3 @@

```javascript
```typescript
import CountDown from 'countdown-pro';

@@ -36,8 +38,8 @@

time: 60 * 1000,
format: time => time/1000,
onChange: time => {
interval: 1000,
onChange: (time) => {
console.log(time);
},
onEnd: () => {
console.log("结束");
console.log('结束');
}

@@ -51,48 +53,36 @@ });

参数 | 说明 | 类型 | 必填 | 默认值
------------- | ------------- | ------------- | ------------- | -------------
time | 倒计时,单位毫秒 | `number` | `Y` | `0`
interval | 时间间隔,单位毫秒 | `number` | - | `1000`
format | 时间格式化,用于格式化 `onChange` 回调数据。必须要有返回值 `(timestamp)=>any` | `function` | - | -
onChange | 时间变化时触发 `(time)=>void` | `function` | - | -
onEnd | 倒计时结束时触发 | `function` | - | -
| 参数 | 说明 | 类型 | 必填 | 默认值 |
| -------- | ------------------ | ----------------------- | ---- | ------ |
| time | 倒计时,单位毫秒 | `number` | `Y` | `0` |
| interval | 时间间隔,单位毫秒 | `number` | - | `1000` |
| onChange | 每次时间间隔时触发 | `(currentTime) => void` | - | - |
| onEnd | 倒计时结束时触发 | `() => void` | - | - |
## 实例方法
方法名 | 说明
------------- | -------------
start | 开始倒计时
pause | 暂停倒计时
reset | 重设倒计时
| 方法名 | 说明 |
| ------ | ---------- |
| start | 开始倒计时 |
| pause | 暂停倒计时 |
| reset | 重置倒计时 |
---
## 静态方法
## 格式化工具方法 `countdown-pro/lib/util`
内置一些简单日期格式方法,通过 `CountDown.方法名` 直接调用。
### 使用
### CountDown.format(timestamp, pattern='HH:mm:ss')
```javascript
import { format, padZero, parseTimeData, parseFormat } from 'countdown-pro/lib/util';
```
如果在浏览器中使用 `script` 标签直接引入文件,可使用全局变量 `countdownUtil` 。
### format(timestamp, formatStr='HH:mm:ss')
> 格式化时间,返回格式化后的时间字符串
```javascript
format(2*60*60*1000);
// => "02:00:00"
format(2*60*60*1000, 'mm:ss');
// => "120:00"
CountDown.format(2 * 60 * 60 * 1000); // "02:00:00"
CountDown.format(2 * 60 * 60 * 1000, 'mm:ss'); // "120:00"
```
参数 | 说明 | 类型 | 必填 | 默认值
------------- | ------------- | ------------- | ------------- | -------------
timestamp | 时间戳,单位毫秒 | `number` | `Y` | -
formatStr | 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒 | `string` | - | `HH:mm:ss`
| 参数 | 说明 | 类型 | 必填 | 默认值 |
| --------- | ---------------------------------------------- | -------- | ---- | ---------- |
| timestamp | 时间戳,单位毫秒 | `number` | `Y` | - |
| pattern | 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒 | `string` | - | `HH:mm:ss` |
### padZero(num, targetLength=2)
### CountDown.padZero(num, targetLength=2)

@@ -102,9 +92,8 @@ > 前置补零,返回补零后的值

```javascript
padZero(2);
// => "02"
CountDown.padZero(2); // "02"
```
### parseTimeData(timestamp)
### CountDown.parseTimeData(timestamp)
> 解析时间戳,返回的时间格式 `timeData`
> 解析时间戳,返回的时间对象格式 `timeData`

@@ -122,15 +111,7 @@ ```typescript

```javascript
parseTimeData(2*60*60*1000);
// => {days: 0, hours: 2, minutes: 0, seconds: 0, milliseconds: 0}
CountDown.parseTimeData(2 * 60 * 60 * 1000); // {days: 0, hours: 2, minutes: 0, seconds: 0, milliseconds: 0}
```
### parseFormat(formatStr, timeData)
> 格式化时间格式 `timeData`
```javascript
parseFormat('mm:ss', {days: 0, hours: 2, minutes: 0, seconds: 0, milliseconds: 0});
// => "120:00"
```
[site]: https://caijf.github.io/countdown/site/
[site]: https://caijf.github.io/countdown/examples/
[npm]: https://img.shields.io/npm/v/countdown-pro.svg
[npm-url]: https://npmjs.com/package/countdown-pro
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD (Register as an anonymous module)
define([], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
module.exports = factory();
} else {
// Browser globals
root.CountDown = factory();
}
}(this, function () {
var noop = function () { };
var isFunction = function (fn) {
return typeof fn === 'function';
}
function CountDown(options) {
this._init(typeof options !== 'object' ? {} : options);
}
CountDown.prototype._init = function (options) {
this.counting = false;
this.conpleted = false;
this.timer = null;
this.options = {
time: 0,
interval: 1000,
format: null,
onChange: noop,
onEnd: noop
};
for (var prop in options) {
if (options.hasOwnProperty(prop)) {
this.options[prop] = options[prop];
}
}
this._initTime();
}
CountDown.prototype._initTime = function () {
this.time = this.options.time;
}
CountDown.prototype._handleChange = function () {
var onChange = this.options.onChange;
var format = this.options.format;
if (isFunction(onChange)) {
onChange(isFunction(format) ? format(this.time) : this.time);
}
}
CountDown.prototype._handleEnd = function () {
var onEnd = this.options.onEnd;
this.pause();
this.conpleted = true;
if (isFunction(onEnd)) {
onEnd();
}
}
CountDown.prototype._tick = function () {
var that = this;
var interval = that.options.interval;
if (that.conpleted) {
that._handleEnd();
return;
}
that.timer = setTimeout(function () {
that.time -= interval;
if (that.time < 0) {
that.time = 0;
}
that._handleChange();
if (that.time <= 0) {
that._handleEnd();
} else {
that._tick();
}
}, interval);
}
// 开始
CountDown.prototype.start = function () {
if (this.counting) {
return;
}
this.counting = true;
this._tick();
}
// 暂停
CountDown.prototype.pause = function () {
clearTimeout(this.timer);
this.counting = false;
}
// 重置
CountDown.prototype.reset = function () {
this.pause();
this.conpleted = false;
this._initTime();
this._handleChange();
}
return CountDown;
}));
!function(t,i){"function"==typeof define&&define.amd?define([],i):"object"==typeof exports?module.exports=i():t.CountDown=i()}(this,function(){function n(){}function o(t){return"function"==typeof t}function t(t){this._init("object"!=typeof t?{}:t)}return t.prototype._init=function(t){for(var i in this.counting=!1,this.conpleted=!1,this.timer=null,this.options={time:0,interval:1e3,format:null,onChange:n,onEnd:n},t)t.hasOwnProperty(i)&&(this.options[i]=t[i]);this._initTime()},t.prototype._initTime=function(){this.time=this.options.time},t.prototype._handleChange=function(){var t=this.options.onChange,i=this.options.format;o(t)&&t(o(i)?i(this.time):this.time)},t.prototype._handleEnd=function(){var t=this.options.onEnd;this.pause(),this.conpleted=!0,o(t)&&t()},t.prototype._tick=function(){var t=this,i=t.options.interval;t.conpleted?t._handleEnd():t.timer=setTimeout(function(){t.time-=i,t.time<0&&(t.time=0),t._handleChange(),t.time<=0?t._handleEnd():t._tick()},i)},t.prototype.start=function(){this.counting||(this.counting=!0,this._tick())},t.prototype.pause=function(){clearTimeout(this.timer),this.counting=!1},t.prototype.reset=function(){this.pause(),this.conpleted=!1,this._initTime(),this._handleChange()},t});
// refs:
// https://github.com/youzan/vant-weapp/blob/dev/packages/count-down/utils.ts
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD (Register as an anonymous module)
define([], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
module.exports = factory();
} else {
// Browser globals
root.countdownUtil = factory();
}
}(this, function () {
function padZero(num, targetLength) {
var str = num + '';
var len = targetLength || 2;
while (str.length < len) {
str = '0' + str;
}
return str;
}
var SECOND = 1000;
var MINUTE = 60 * SECOND;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
function parseTimeData(time) {
var ret = {};
ret.days = Math.floor(time / DAY);
ret.hours = Math.floor((time % DAY) / HOUR);
ret.minutes = Math.floor((time % HOUR) / MINUTE);
ret.seconds = Math.floor((time % MINUTE) / SECOND);
ret.milliseconds = Math.floor(time % SECOND);
return ret;
}
function parseFormat(format, timeData) {
var days = timeData.days,
hours = timeData.hours,
minutes = timeData.minutes,
seconds = timeData.seconds,
milliseconds = timeData.milliseconds;
if (format.indexOf('DD') === -1) {
hours += days * 24;
} else {
format = format.replace('DD', padZero(days));
}
if (format.indexOf('HH') === -1) {
minutes += hours * 60;
} else {
format = format.replace('HH', padZero(hours));
}
if (format.indexOf('mm') === -1) {
seconds += minutes * 60;
} else {
format = format.replace('mm', padZero(minutes));
}
if (format.indexOf('ss') === -1) {
milliseconds += seconds * 1000;
} else {
format = format.replace('ss', padZero(seconds));
}
return format.replace('SSS', padZero(milliseconds, 3));
}
function format(timestamp, formatStr) {
var timeData = parseTimeData(timestamp);
return parseFormat(formatStr || "HH:mm:ss", timeData);
}
return {
format: format,
padZero: padZero,
parseTimeData: parseTimeData,
parseFormat: parseFormat
}
}));
!function(e,n){"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?module.exports=n():e.countdownUtil=n()}(this,function(){function f(e,n){for(var o=e+"",r=n||2;o.length<r;)o="0"+o;return o}var o=1e3,r=60*o,t=60*r,s=24*t;function a(e){var n={};return n.days=Math.floor(e/s),n.hours=Math.floor(e%s/t),n.minutes=Math.floor(e%t/r),n.seconds=Math.floor(e%r/o),n.milliseconds=Math.floor(e%o),n}function i(e,n){var o=n.days,r=n.hours,t=n.minutes,s=n.seconds,a=n.milliseconds;return-1===e.indexOf("DD")?r+=24*o:e=e.replace("DD",f(o)),-1===e.indexOf("HH")?t+=60*r:e=e.replace("HH",f(r)),-1===e.indexOf("mm")?s+=60*t:e=e.replace("mm",f(t)),-1===e.indexOf("ss")?a+=1e3*s:e=e.replace("ss",f(s)),e.replace("SSS",f(a,3))}return{format:function(e,n){return i(n||"HH:mm:ss",a(e))},padZero:f,parseTimeData:a,parseFormat:i}});
!function(t,i){"function"==typeof define&&define.amd?define([],i):"object"==typeof exports?module.exports=i():t.CountDown=i()}(this,function(){function n(){}function o(t){return"function"==typeof t}function t(t){this._init("object"!=typeof t?{}:t)}return t.prototype._init=function(t){for(var i in this.counting=!1,this.conpleted=!1,this.timer=null,this.options={time:0,interval:1e3,format:null,onChange:n,onEnd:n},t)t.hasOwnProperty(i)&&(this.options[i]=t[i]);this._initTime()},t.prototype._initTime=function(){this.time=this.options.time},t.prototype._handleChange=function(){var t=this.options.onChange,i=this.options.format;o(t)&&t(o(i)?i(this.time):this.time)},t.prototype._handleEnd=function(){var t=this.options.onEnd;this.pause(),this.conpleted=!0,o(t)&&t()},t.prototype._tick=function(){var t=this,i=t.options.interval;t.conpleted?t._handleEnd():t.timer=setTimeout(function(){t.time-=i,t.time<0&&(t.time=0),t._handleChange(),t.time<=0?t._handleEnd():t._tick()},i)},t.prototype.start=function(){this.counting||(this.counting=!0,this._tick())},t.prototype.pause=function(){clearTimeout(this.timer),this.counting=!1},t.prototype.reset=function(){this.pause(),this.conpleted=!1,this._initTime(),this._handleChange()},t});
interface TimeData {
days: number;
hours: number;
minutes: number;
seconds: number;
milliseconds: number;
}
declare module 'countdown-pro/lib/util' {
export function format(timestamp: number, formatStr?: string): string;
export function padZero(num: number, targetLength?: number): string;
export function parseTimeData(timestamp: number): TimeData;
export function parseFormat(formatStr: string, timeData: TimeData): string;
}
!function(e,n){"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?module.exports=n():e.countdownUtil=n()}(this,function(){function f(e,n){for(var o=e+"",r=n||2;o.length<r;)o="0"+o;return o}var o=1e3,r=60*o,t=60*r,s=24*t;function a(e){var n={};return n.days=Math.floor(e/s),n.hours=Math.floor(e%s/t),n.minutes=Math.floor(e%t/r),n.seconds=Math.floor(e%r/o),n.milliseconds=Math.floor(e%o),n}function i(e,n){var o=n.days,r=n.hours,t=n.minutes,s=n.seconds,a=n.milliseconds;return-1===e.indexOf("DD")?r+=24*o:e=e.replace("DD",f(o)),-1===e.indexOf("HH")?t+=60*r:e=e.replace("HH",f(r)),-1===e.indexOf("mm")?s+=60*t:e=e.replace("mm",f(t)),-1===e.indexOf("ss")?a+=1e3*s:e=e.replace("ss",f(s)),e.replace("SSS",f(a,3))}return{format:function(e,n){return i(n||"HH:mm:ss",a(e))},padZero:f,parseTimeData:a,parseFormat:i}});
interface CountDownOptions {
time: number;
interval?: number;
format?: (timestamp: number) => any;
onChange?: (time: any) => void;
onEnd?: () => void;
}
declare class CountDown {
constructor(options: CountDownOptions);
start(): void;
pause(): void;
reset(): void;
}
export default CountDown;
interface TimeData {
days: number;
hours: number;
minutes: number;
seconds: number;
milliseconds: number;
}
declare module 'countdown-pro/lib/util' {
export function format(timestamp: number, formatStr?: string): string;
export function padZero(num: number, targetLength?: number): string;
export function parseTimeData(timestamp: number): TimeData;
export function parseFormat(formatStr: string, timeData: TimeData): string;
}