JavaScript – throttle and debounce functions

A couple years ago the notion of managing JS execution with timers was in vogue – IDK what caused that particular wave of interest, but I remember doing the same thing to mousemoves around 2001/2002 (when it really mattered – you could actually watch the caret blink less frantically as your throttling function upped it’s […]

jQuery (or anything else) basics of dragging

Here’s the most basic implementation of dragging I’m aware of… I’ll present an example usin jQuery, but the same logic can be used in any JS framework, raw JS, or even ActionScript, Java, or any other language or technology that processes similar user events and provided a UI. Here’s the idea: On mousedown: 1. record […]

HTML5 Audio – show duration before load

If you’re trying to display current time / time remaining / total time of an HTML5 audio element before it’s loaded, you can use the loadedmetadata listener. You can usually use the same listener that’s being used for the timeupdate handler. var displayTime = function(){ // use audio.currentTime and audio.duration to format and present that […]

MooTools – fireEvent passing a single array as a parameter

The MooTools fireEvent method allows us to pass extra parameters pretty simply. To pass a single parameter: myInstance.fireEvent("eventType", myVar); To pass multiple parameters, we have to use an array: myInstance.fireEvent("eventType", [myFirstVar, mySecondVar]); However, what if you want to pass a single parameter that happens to be an array? For example, if you have this callback: […]

MooTools: Long-range dispatcher

While working on a very large ajax-y application, I found myself facing the following scenario: 1. Tons of custom (non-DOM) events. 2. These events are firing from very deeply-nested classes. 3. I wanted these events to communicate with “long-range” instances, far outside the currently executing scope block. 4. I wanted to preserve the scope of […]

JavaScript (jQuery + MooTools) Simple Swipe Event

As popular as mobile browsing has become lately, I haven’t found a much in the way of solid swipe management libraries, for any of the major frameworks. Fortunately, the logic is pretty simple. The basic idea is this: 1. When the mouse/finger goes down, measure where it hit. 2. When the mouse/finger goes back up, […]

JavaScript (jQuery + MooTools) get WordPress Posts from RSS as JSON

The url is structured like so:[encoded URL of RSS feed] Replace [encoded URL of RSS feed] with the URL of the WP blog you want to fetch posts from. I’ll use mine ( for the samples. With either method, you’ll get back an object with the following keys: responseData responseDetails responseStatus responseData has a […]

JavaScript (jQuery + MooTools) get tweets from twitter feed

The url is structured like so:[twitter handle].json Replace [twitter handle] with the user whose tweets you’re requesting. I’ll use mine for the samples. With either method, you’ll get back an indexed array of objects (so the most recent tweet would be data[0]). Each object has the following keys: in_reply_to_user_id in_reply_to_user_id_str contributors in_reply_to_status_id created_at user […]

MooTools – get currentTarget method

Consider the following example (which comes up a lot in my style of writing Moo): You create a new Class and use the Binds property to fix the scope of it’s methods to the Class instance. You have a DOM element with some data .stored, and a mouse event handler that is a method Bound […]

jQuery + MooTools – wrapped text to lines

Converting wrapped text to lines (either for line numbering or to break each line into individual elements) is a question that comes up on the boards quite a bit, and I finally had a need to do so, so here it is (for both MooTools and jQuery – the jQuery version is not tested, so […]