Nourished Personal Mucus
    Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    time-calcpublic

    time-calc

    Build status

    Support for calculating time-unit like twitter You can use this both node and browsers.

    Install

    Install with npm:

        npm install time-calc

    API

    unit keys
    Y, M, D, h, m, s, ms

    - calculate time-unit

        var watcher = require('time-calc')();
        setTimeout(function(){
          console.log(watcher()); // => '30ms' 
        }, 30);
        setTimeout(function(){
          console.log(watcher()); // => '1s' 
        }, 1200);
        setTimeout(function(){
          console.log(watcher()); // => '1m' 
        }, 61000);

    - limit target unit

        var watcher = require('time-calc')();
        setTimeout(function({enable:{s:false}}){
          console.log(watcher()); // => '1100ms' 
        }, 1100);
        setTimeout(function(){
          console.log(watcher({enable:{m:false}})); // => '60s' 
        }, 61000);

    - change display

        var watcher = timecalc({display:{s:'', m: ''}});
        setTimeout(function(){
          console.log(watcher()); // => '1秒' 
        }, 1000);
        setTimeout(function(){
          console.log(watcher()); // => '1分' 
        }, 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()); // => 'number' 

    - set start position

        var watcher = require('time-calc')({basepoint: Date.now() + 5000});
        setTimeout(function(){
          console.log(watcher()); // => '1s' 
        }, 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})); // => '1s' 
        }, 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()); // => '1m' 
        }, 61000);
     
    </script> 

    install

    npm i time-calc

    Downloadsweekly downloads

    11

    version

    0.7.4

    license

    none

    repository

    github.com

    last publish

    collaborators

    • avatar