npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@ionepub/node-timer

v1.0.0

Published

A simple countdown timer module.

Readme

node-timer.js nodejs秒倒计时插件

本插件目的在于方便在nodejs中使用秒级的倒计时。

js版参见:https://github.com/ionepub/js-timer

安装

npm i @ionepub/node-timer --save

使用方法

基本使用

var Timer = require('@ionepub/node-timer');
Timer.run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
	if(is_end){
		console.log("已结束");
	}
});

配置

插件有4个配置项:

  • time 倒计时秒数,整数,默认60(秒)
  • format 返回的倒计时数字格式,默认string字符串,返回的数字格式为两位字符,如09;可选值int,返回整数,如9
  • withHour 返回的倒计时是否计算小时,默认true,可选值false
  • withDay 返回的倒计时是否计算天,默认true,可选值false

需要注意的是,即使withHourwithDay设置为false,在回调函数的返回值中,依然会返回dayhour参数,但是参数值始终为零(即不计算)。

自定义配置的几个方法:

#1 使用Timer.init()方法

// 使用默认配置
Timer.init();

// 倒计时30秒
Timer.init({time:30});

// 其他例子
Timer.init({time:30, format: 'int', withHour: true, withDay: false});

#2 直接设置插件暴露的变量

Timer.settings.time = 30;
Timer.settings.withDay = false;

#3 使用Timer.run()方法

Timer.run()方法的第一个参数也支持自定义配置

// 使用默认配置
Timer.run();

// 倒计时30秒
Timer.run({time:30});

// 其他例子
Timer.run({time:30, format: 'int', withHour: true, withDay: false});

Timer.run({time:30, format: 'int', withHour: true, withDay: false}, function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
	if(is_end){
		console.log("已结束");
	}
});

Timer.run()

Timer.run()方法支持1-2个参数,第一个参数为配置项对象或回调方法;当第一个参数为配置项时,第二个参数为回调方法。

// 使用默认配置
Timer.run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
	if(is_end){
		console.log("已结束");
	}
});

// 无回调
Timer.run({time:30, format: 'int', withHour: true, withDay: false});

// 自定义配置和回调
Timer.run({time:30, format: 'int', withHour: true, withDay: false}, function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
	if(is_end){
		console.log("已结束");
	}
});

Timer.diff()

为了更方便使用,插件提供了diff()方法,支持填入开始时间(秒)和结束时间参数,以此设置倒计时瞄数。

diff()方法必须传递两个整数,且startTime必须小于endTime

Timer.diff(1516676419, 1516676457);

Timer.diff(0, 30).run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
});

链式调用

插件的init()run()diff()方法均支持链式调用,但是需要注意一下调用顺序。

  • 如果init()或者diff()run()之后,这个方法的设置是无效的
  • 如果多个方法中都对同一个配置项操作,则后面的操作会覆盖前面的设置
Timer.init().run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
});

Timer.init({time:30}).run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
});

Timer.diff(0, 30).run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
});

分步调用也是可以的:

// 倒计时30秒
Timer.init({time:30});
Timer.run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
});

// 倒计时40秒
Timer.diff(0, 40);
Timer.run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
});

多个倒计时

插件支持在同一页面中有多个倒计时,例如:

Timer.diff(0,10).run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
	if(is_end){
		console.log("已结束");
	}
})

Timer.diff(0,5).run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
	if(is_end){
		console.log("已结束");
	}
})