JSPM

@ionepub/node-timer

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q32590F
  • License MIT

A simple countdown timer module.

Package Exports

  • @ionepub/node-timer

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@ionepub/node-timer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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("已结束");
    }
})