time-calc
Support for calculating time-unit like twitter
You can use this both node and browsers.
Install
Install with npm:
API
unit keys
Y, M, D, h, m, s, ms
- calculate time-unit
var watcher = require('time-calc')();
setTimeout(function(){
console.log(watcher());
}, 30);
setTimeout(function(){
console.log(watcher());
}, 1200);
setTimeout(function(){
console.log(watcher());
}, 61000);
- limit target unit
var watcher = require('time-calc')();
setTimeout(function({enable:{s:false}}){
console.log(watcher());
}, 1100);
setTimeout(function(){
console.log(watcher({enable:{m:false}}));
}, 61000);
- change display
var watcher = timecalc({display:{s:'秒', m: '分'}});
setTimeout(function(){
console.log(watcher());
}, 1000);
setTimeout(function(){
console.log(watcher());
}, 60000);
- change viewer
default
function defaultDisp(diff, repl) {
if(repl.indexOf('%d') >= 0)
return repl.replace(/[^\\]%d/g, split3(diff));
if(repl.indexOf('%D') == 0)
return repl.substr(2);
return split3(diff) + repl;
}
var watcher = require('time-calc');
watcher.viewer(function(diff, repl){ return diff });
console.log(typeof watcher());
- set start position
var watcher = require('time-calc')({basepoint: Date.now() + 5000});
setTimeout(function(){
console.log(watcher());
}, 6200);
- watcher can accept all same options as time-calc
var watcher = require('time-calc')(), base = Date.now() + 5000;
setTimeout(function(){
console.log(watcher({basepoint: base}));
}, 6200);
also use on browser
<script type="text/javascript" src="time-calc.js"></script>
<script type="text/javascript">
var watcher = timecalc();
setTimeout(function(){
console.log(watcher());
}, 61000);
</script>