Skip to content

Instantly share code, notes, and snippets.

@benwei
Created December 7, 2012 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benwei/4232236 to your computer and use it in GitHub Desktop.
Save benwei/4232236 to your computer and use it in GitHub Desktop.
SpentTime class for node js
function SpentTime () {
this.reset = function () {
start = new Date();
}
this.diff = function () {
var now = new Date();
return (now.getTime() - start.getTime())
}
var start = new Date();
}
module.exports = SpentTime;
var SpentTime = require('./lib/SpentTime');
var st = new SpentTime();
setTimeout(function () {
console.log("test 1 in " + st.diff() + 'ms');
setTimeout(function () {
console.log("test 2 in " + st.diff() + 'ms');
st.reset();
setTimeout(function () {
console.log("after reset(), test 3 in " + st.diff() + 'ms');
st.reset();
setTimeout(function () {
// for more accurately, store diff in variable, because of console.log need time ?;
var d = st.diff();
console.log("after reset() and variable, test 4 in " + d + 'ms');
}, 15);
}, 15);
}, 20);
}, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment