Last active
December 21, 2015 04:49
-
-
Save abi/6252739 to your computer and use it in GitHub Desktop.
sereneLog lets you log stuff without freaking you and your browser and out. Especially useful for debugging the progress of long loops.
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 (window) { | |
/* Throttle fn modified from underscore.js */ | |
var _throttle = function (func, wait) { | |
var context | |
var args | |
var result | |
var timeout = null | |
var previous = 0 | |
var later = function() { | |
previous = new Date | |
timeout = null | |
result = func.apply(context, args) | |
} | |
return function() { | |
var now = new Date | |
var remaining = wait - (now - previous) | |
context = this | |
args = arguments | |
if (remaining <= 0) { | |
clearTimeout(timeout) | |
timeout = null | |
previous = now | |
result = func.apply(context, args) | |
} else if (!timeout) { | |
timeout = setTimeout(later, remaining) | |
} | |
return result | |
} | |
} | |
/* Export sereneLog */ | |
window.sereneLog = _throttle(function () { | |
var args = Array.prototype.slice.call(arguments) | |
console.log.apply(console, args) | |
}, 100) // Change the time (in ms) here to have more or fewer logs | |
})(window) | |
/* Will print a ton of logs */ | |
var now = Date.now() | |
while (Date.now() - now < 2000) { | |
console.log('foo', 'bar') | |
} | |
/* Will print 20 or 21 logs */ | |
var now = Date.now() | |
while (Date.now() - now < 2000) { | |
sereneLog('foo', 'bar') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment