Created
December 7, 2012 09:59
-
-
Save benwei/4232236 to your computer and use it in GitHub Desktop.
SpentTime class for node js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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