🎩 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

A simple countdown.

Source
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

CountDown

一个简单的倒计时。

查看示例

使用

npmyarn 安装

npm install countdown-pro
yarn add countdown-pro

浏览器引入

在浏览器中使用 script 标签直接引入文件,并使用全局变量 CountDown

  • 该仓库的 dist 目录下提供了 countdown.js 以及 countdown.min.js
  • npm 包的 countdown-pro/dist 目录下也提供了 countdown.js 以及 countdown.min.js
  • 你也可以通过 UNPKG 进行下载。

示例

import CountDown from 'countdown-pro';

const countdown = new CountDown({
  time: 60 * 1000,
  format: time => time/1000,
  onChange: time => {
    console.log(time);
  },
  onEnd: () => {
    console.log("结束");
  }
});

countdown.start();

配置项

参数说明类型必填默认值
time倒计时,单位毫秒numberY0
interval时间间隔,单位毫秒number-1000
format时间格式化,用于格式化 onChange 回调数据。必须要有返回值 (timestamp)=>anyfunction--
onChange时间变化时触发 (time)=>voidfunction--
onEnd倒计时结束时触发function--

实例方法

方法名说明
start开始倒计时
pause暂停倒计时
reset重设倒计时

格式化工具方法 countdown-pro/lib/util

使用

import { format, padZero, parseTimeData, parseFormat } from 'countdown-pro/lib/util';

如果在浏览器中使用 script 标签直接引入文件,可使用全局变量 countdownUtil

format(timestamp, formatStr='HH:mm:ss')

格式化时间,返回格式化后的时间字符串

format(2*60*60*1000);
// => "02:00:00"

format(2*60*60*1000, 'mm:ss');
// => "120:00"
参数说明类型必填默认值
timestamp时间戳,单位毫秒numberY-
formatStr时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒string-HH:mm:ss

padZero(num, targetLength=2)

前置补零,返回补零后的值

padZero(2);
// => "02"

parseTimeData(timestamp)

解析时间戳,返回的时间格式 timeData

interface TimeData {
  days: number; // 天数
  hours: number; // 小时
  minutes: number; // 分钟
  seconds: number; // 秒数
  milliseconds: number; // 毫秒
}
parseTimeData(2*60*60*1000);
// => {days: 0, hours: 2, minutes: 0, seconds: 0, milliseconds: 0}

parseFormat(formatStr, timeData)

格式化时间格式 timeData

parseFormat('mm:ss', {days: 0, hours: 2, minutes: 0, seconds: 0, milliseconds: 0});
// => "120:00"

Keywords

countdown

FAQs

Package last updated on 03 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts