Skip to content
Browse files

added method .every(nanosecond, callback)

  • Loading branch information...
1 parent a9b9ce8 commit 1c923a9254bc7eb44534f9d849057359567ed945 NHQ SERVER ADMIN committed Sep 14, 2012
Showing with 61 additions and 28 deletions.
  1. +14 −11 README.md
  2. +4 −5 examples/sinceBegin.js
  3. +7 −5 examples/sinceLast.js
  4. +4 −5 examples/ticktock.js
  5. +30 −0 index.js
  6. +2 −2 package.json
View
25 README.md
@@ -1,23 +1,26 @@
-**A simple timing module that uses process.hrtime**
+**Timing functions that use process.hrtime**
npm install since-when
usage
- var T = require('since-when')
- , time = new T()
- ;
-
- setInterval(tick, 1500)
+ var Time = require('../');
+ var time = new Time();
+
+ time.every(1e9, tick)
+ // every billion nanosecond tock()
- function tick(){
- console.log(time.sinceLast()) // returns [seconds, nanoseconds]
+ function tick(tock, interval){
+ console.log(interval + ' time in nanoseconds passed')
+ tock()
}
-
+
__METHODS__
-**T.sinceBegin** - time since new T()
+**T.sinceBegin()** - time since new T()
+
+**T.sinceLast()** - time since last tick, starts at new T()
-**T.sinceLast** - time since last tick, starts at new T()
+**T.every(nanoseconds, function)** - call function(nextTick, interval) every nanoseconds. function must call nextTick()
see /examples
View
9 examples/sinceBegin.js
@@ -1,9 +1,8 @@
-var T = require('../')
- , time = new T()
- ;
+var T = require('../');
+var time = new T();
-setInterval(tick, 500)
+setInterval(tick, 500);
function tick(){
console.log(time.sinceBegin())
-}
+};
View
12 examples/sinceLast.js
@@ -1,9 +1,11 @@
-var T = require('../')
- , time = new T()
- ;
+var T = require('../');
+var time = new T();
-setInterval(tick, 1500)
+setInterval(tick, 0);
function tick(){
console.log(time.sinceLast())
-}
+};
+
+
+
View
9 examples/ticktock.js
@@ -1,11 +1,10 @@
-var Time = require('../')
- , time = new Time()
- ;
+var Time = require('../');
+var time = new Time();
-time.every(1e9, tock)
+time.every(1e9, tock);
// every billion nanosecond tock()
function tock(tick, interval){
console.log(interval + ' time in nanoseconds passed')
tick()
-}
+};
View
30 index.js
@@ -20,6 +20,36 @@ T.prototype.sinceLast = function(){
return this.x
}
+T.prototype.every = function(ns, fn){
+ this.beat = process.hrtime()
+ var int = ns || 0
+ , fn = fn || function(t,c){c()}
+ , self = this;
+
+ function tick(){
+ process.nextTick(tock)
+ }
+
+ function tock(){
+ var ns = nanos(process.hrtime(self.beat))
+// console.log(ns)
+ if (ns > int){
+ self.beat = process.hrtime();
+ fn(tick, ns)
+ }
+ else tick()
+ }
+
+ tick()
+}
+
+function nanos(arr){
+ return arr[0] * 1e9 + arr[1]
+}
+
+function seconds(arr){
+ return arr[0] + (1e9 / arr[1])
+}
function add(a, b){
var ns = a[1] + b[1];
b[0] += a[0];
View
4 package.json
@@ -1,7 +1,7 @@
{
"name": "since-when",
- "description": "timing helpers using process.hrtime",
- "version": "0.2.0",
+ "description": "timing functions using process.hrtime",
+ "version": "0.4.0",
"author": "Johnny Honestly <mostmodernist@gmail.com>",
"dependencies": {},
"devDependencies": {},

0 comments on commit 1c923a9

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