Skip to content

Instantly share code, notes, and snippets.

@mkuklis
Created November 8, 2012 15:31
Show Gist options
  • Save mkuklis/4039482 to your computer and use it in GitHub Desktop.
Save mkuklis/4039482 to your computer and use it in GitHub Desktop.
logger.js
(function () {
"use strict";
var Logger = this.Logger = function ($el) {
this.$logger = $el;
this.timers = {};
}
Logger.prototype.stop = function (label) {
this.timers[label].end = new Date().getTime();
this.timers[label].time = this.timers[label].end - this.timers[label].start;
this.log(label + ': ' + this.timers[label].time + 'ms');
}
Logger.prototype.start = function (label) {
this.timers[label] = {};
this.timers[label].start = new Date().getTime();
}
Logger.prototype.clear = function () {
this.timers = {};
this.$logger.html('');
}
Logger.prototype.log = function (message) {
this.$logger.append('<p>' + message + '</p>');
}
}).call(this);
// usage
var logger = new Logger($('#logger'));
logger.start('label1');
logger.stop('label1'); // prints time taken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment