Skip to content

Instantly share code, notes, and snippets.

@benhodgson
benhodgson / index.html
Created May 28, 2013 17:47
Playing about with ondevicemotion in MobileSafari on iOS to detect free fall
<!DOCTYPE html>
<html>
<head>
<title>Freefall detection test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div>
accel: x, y, z
</div>
function whenAllTriggered(tokens, callback) {
// returns a function that takes a single token
// once this function has been called with all tokens at least once,
// callback is invoked.
var triggeredStates = {},
nTokens = 0,
nTriggered = 0;
// Hey, @addyosmani! Here's my #protip.
// Of all the JavaScript I've written in the past year, this is definitely
// my favourite. It uses some of JavaScript's awesome functional features
// (namely closure and functions as first-class objects) to create a clean
// interface for building specific event listeners from a general form.
// This particular snippet allows you to "subdue" the behaviour of some of
// the more excitable DOM events such as scroll and resize. With scroll, for
// example, your event listener will be called up to once for *every pixel*
@benhodgson
benhodgson / github-repo-stats.js
Created November 1, 2011 18:17
Fetch the Watchers and Forks counts for a GitHub repo
// What up, @mdo. You'll need a recent version of jQuery.
// You can try this out by pasting it into the console on http://api.jquery.com/jQuery.ajax/
window.repoCallback = function (obj) {
console.log("Watchers:" + obj['repository']['watchers']);
console.log("Forks:" + obj['repository']['forks']);
}
$.ajax("http://github.com/api/v2/json/repos/show/twitter/bootstrap?callback=repoCallback", {dataType: "jsonp"});
@benhodgson
benhodgson / pythonrc.py
Created May 4, 2011 12:41
Add history between sessions and auto-completion via the ESC and tab keys to the interactive Python interpreter with this dot-file.
"""
Python Startup File (from https://gist.github.com/955154)
Add this file to ~/.pythonrc.py to add history between sessions and
auto-completion via the ESC key to the interactive Python interpreter. After
adding this file, put something like the following line in your .bash_profile:
export PYTHONSTARTUP=$HOME/.pythonrc.py
Requires a recent version of Python and the readline package, which you can
<!DOCTYPE html>
<style>
html {
height: 100%;
}
body{
height: 100%;
}
.myDiv {
height: 100%;
@benhodgson
benhodgson / getjQuerySelector.js
Created October 30, 2010 00:34
Builds a jQuery selector string that selects the given DOM element (adapted from http://snippets.dzone.com/posts/show/3754)
var getjQuerySelector = (function() {
function siblingIndexPseudoSelector(el) {
var sibsBefore = 0,
sibsAfter = 0,
sib;
for (sib = el.previousSibling; sib; sib = sib.previousSibling) {
if(sib.nodeType == 1 && sib.tagName == el.tagName) {
sibsBefore += 1;
}
// Hello! I am Ben Hodgson.
// 1: how could you rewrite the following to make it shorter?
if (foo) {
bar.doSomething(el);
} else {
bar.doSomethingElse(el);
}
// A:
PROJECTS_DIR=$HOME/projects
function workon {
cd $PROJECTS_DIR/"$1"/;
cd "$1" > /dev/null 2>&1 && source ../bin/activate > /dev/null 2>&1;
}
complete -W '`ls $PROJECTS_DIR`' workon