Updates to Truncation Script
I made a few improvements to my Firefox truncation method this past weekend:
- No longer depends on YUI to run. Since it's Firefox-only, I can assume a few nifty JS bits like
getComputedStyle()andgetElementsByClassName(). - Improved the truncation algorithm. I was simply lopping off characters until it was the right length, but that performed very poorly, for obvious reasons. I'm now doing a binary "search" for the right length, which runs great (in my opinion).
onresize"debounce": I've written a little helper method that wraps the event handler for theresizeevent. When resizing the browser window, the event is fired every time the mouse is moved, causing a load of lag. The helper method limits how often the event handler fires, and makes sure it stops firing once the resize is over.
Here's the source of the debouncer:
function debounce(fn, ms, ctxt) {
var ctx = ctxt || window;
var it, to, del = ms, fun = fn;
return function () {
var args = arguments;
if (!it) {
it = setInterval(function () {
fun.apply(ctx, args);
},del);
}
clearTimeout(to);
to = setTimeout(function() {
clearInterval(it);
it = false;
},del);
};
}
To use it:
document.onresize = debounce(handler, 100);
Matt "Potch" Claypotch is a web developer (At Mozilla!), geek, and all-around swell guy who currently resides in Mountain View, California.