Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added method .every(nanosecond, callback)

  • Loading branch information...
commit 1c923a9254bc7eb44534f9d849057359567ed945 1 parent a9b9ce8
NHQ SERVER ADMIN authored
25 README.md
Source Rendered
... ... @@ -1,23 +1,26 @@
1   -**A simple timing module that uses process.hrtime**
  1 +**Timing functions that use process.hrtime**
2 2
3 3 npm install since-when
4 4
5 5 usage
6 6
7   - var T = require('since-when')
8   - , time = new T()
9   - ;
10   -
11   - setInterval(tick, 1500)
  7 + var Time = require('../');
  8 + var time = new Time();
  9 +
  10 + time.every(1e9, tick)
  11 + // every billion nanosecond tock()
12 12
13   - function tick(){
14   - console.log(time.sinceLast()) // returns [seconds, nanoseconds]
  13 + function tick(tock, interval){
  14 + console.log(interval + ' time in nanoseconds passed')
  15 + tock()
15 16 }
16   -
  17 +
17 18 __METHODS__
18 19
19   -**T.sinceBegin** - time since new T()
  20 +**T.sinceBegin()** - time since new T()
  21 +
  22 +**T.sinceLast()** - time since last tick, starts at new T()
20 23
21   -**T.sinceLast** - time since last tick, starts at new T()
  24 +**T.every(nanoseconds, function)** - call function(nextTick, interval) every nanoseconds. function must call nextTick()
22 25
23 26 see /examples
9 examples/sinceBegin.js
... ... @@ -1,9 +1,8 @@
1   -var T = require('../')
2   - , time = new T()
3   - ;
  1 +var T = require('../');
  2 +var time = new T();
4 3
5   -setInterval(tick, 500)
  4 +setInterval(tick, 500);
6 5
7 6 function tick(){
8 7 console.log(time.sinceBegin())
9   -}
  8 +};
12 examples/sinceLast.js
... ... @@ -1,9 +1,11 @@
1   -var T = require('../')
2   - , time = new T()
3   - ;
  1 +var T = require('../');
  2 +var time = new T();
4 3
5   -setInterval(tick, 1500)
  4 +setInterval(tick, 0);
6 5
7 6 function tick(){
8 7 console.log(time.sinceLast())
9   -}
  8 +};
  9 +
  10 +
  11 +
9 examples/ticktock.js
... ... @@ -1,11 +1,10 @@
1   -var Time = require('../')
2   - , time = new Time()
3   - ;
  1 +var Time = require('../');
  2 +var time = new Time();
4 3
5   -time.every(1e9, tock)
  4 +time.every(1e9, tock);
6 5 // every billion nanosecond tock()
7 6
8 7 function tock(tick, interval){
9 8 console.log(interval + ' time in nanoseconds passed')
10 9 tick()
11   -}
  10 +};
30 index.js
@@ -20,6 +20,36 @@ T.prototype.sinceLast = function(){
20 20 return this.x
21 21 }
22 22
  23 +T.prototype.every = function(ns, fn){
  24 + this.beat = process.hrtime()
  25 + var int = ns || 0
  26 + , fn = fn || function(t,c){c()}
  27 + , self = this;
  28 +
  29 + function tick(){
  30 + process.nextTick(tock)
  31 + }
  32 +
  33 + function tock(){
  34 + var ns = nanos(process.hrtime(self.beat))
  35 +// console.log(ns)
  36 + if (ns > int){
  37 + self.beat = process.hrtime();
  38 + fn(tick, ns)
  39 + }
  40 + else tick()
  41 + }
  42 +
  43 + tick()
  44 +}
  45 +
  46 +function nanos(arr){
  47 + return arr[0] * 1e9 + arr[1]
  48 +}
  49 +
  50 +function seconds(arr){
  51 + return arr[0] + (1e9 / arr[1])
  52 +}
23 53 function add(a, b){
24 54 var ns = a[1] + b[1];
25 55 b[0] += a[0];
4 package.json
... ... @@ -1,7 +1,7 @@
1 1 {
2 2 "name": "since-when",
3   - "description": "timing helpers using process.hrtime",
4   - "version": "0.2.0",
  3 + "description": "timing functions using process.hrtime",
  4 + "version": "0.4.0",
5 5 "author": "Johnny Honestly <mostmodernist@gmail.com>",
6 6 "dependencies": {},
7 7 "devDependencies": {},

0 comments on commit 1c923a9

Please sign in to comment.
Something went wrong with that request. Please try again.