Skip to content

Instantly share code, notes, and snippets.

View getify's full-sized avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@getify
getify / browser-net-output.txt
Created October 5, 2011 04:07
trouble getting socket.io to work in node
xx.yy.zz.ww GET 200 application/javascript 234B 72ms
278839520115563000 GET 101 undefined 30B 2.0min
278839520115563000 GET 200 text/plain 159B 2.3min
278839520115563000 GET 200 text/javascript 166B 20.40s
278839520115563000 GET 200 text/javascript 166B 20.06s
@getify
getify / gist:1287385
Created October 14, 2011 15:13
LABjs: testing "ordered execution" bug in Firefox (on linux?)
var script1 = document.createElement("script"),
script2 = document.createElement("script"),
script3 = document.createElement("script"),
script4 = document.createElement("script")
;
script1.async = false;
script1.src = "../js/mustache-min.js";
script2.async = false;
@getify
getify / ex1.js
Created October 21, 2011 13:59
possible API for asyncGate.js... what do you think?
function foo(done) {
setTimeout(done,5000);
}
function bar(done) {
setTimeout(done,1000);
}
function baz() {
alert("all done!");
function flatten_array(arr) {
var i;
for (i=0; i<arr.length; ) {
if (Object.prototype.toString.call(arr[i]) == "[object Array]") {
// prepend `splice()` arguments to `tmp` array, to enable `apply()` call
arr.splice.apply(arr,[i,1].concat(arr[i]));
continue;
}
i++;
<script src="LAB.js"></script>
<script>
// put whatever here, for your page's other scripts that you load with <script> tags.
$LAB.script("my_script1.js").script("my_script2.js").wait().script("my_script3.js");
window.fbAsyncInit = function() {
FB.init({appId: 'your app id', status: true, cookie: true,
xfbml: true});
};
@getify
getify / index.html
Created November 23, 2011 18:17
showing how i use $LAB in my pages -- aka, best-practice recommendation
<!DOCTYPE html>
<html>
<head>...</head>
<body>
...
<script src="load.js"></script>
</body>
</html>
@getify
getify / 1.js
Created June 11, 2012 13:54
exploring scope isolation (sandboxing) in JS
sandbox (JSON) { // explicitly list everything to import from the outer scope
var foobar = "baz"; // scoped to this sandbox
baz = "foobar"; // still scoped to this sandbox, not auto-global.
console.log(JSON); // available!
console.log(window); // not available because not imported
console.log(this); // undefined
var $options = $("<option></option>");
$options.add($("<option></option>"));
$options.add($("<option></option>"));
console.log($options.length); // 1 -- wtf?
console.log($options.html()); // <option></option> -- again, wtf?
// the documentation for jQuery#add seems to indicate this should work but it doesn't. Any ideas?
@getify
getify / gist:2926699
Created June 13, 2012 21:50
non-rectangular (line and circle) clears in canvas
/*
To see a demo of these in use, check out: http://test.getify.com/test-canvas-clear-alt.html
Example uses:
*/
clearCircle(context,/*x=*/120,/*y=*/80,/*radius=*/60);
clearLineSquared(context,/*x1=*/10,/*y1=*/10,/*x2=*/53,/*y2=*/67,/*thickness=*/5);
clearLineRounded(context,/*x1=*/15,/*y1=*/100,/*x2=*/90,/*y2=*/170,/*thickness=*/10);
@getify
getify / code.js
Created June 19, 2012 21:47
implementing CSS3 specificity algorithm (with some extensions) in JS
/**
* calculates a set of specificity scores for a CSS selector
* based on (but extending) the CSS3 specificity algorithm:
* http://reference.sitepoint.com/css/specificity
*
* @param {string} selector
*/
function specificity(selector) {
var specificity = [0,0,0,0,0], idx, matches, tmp = 0,
selectors = selector.split(/\s*,\s*/)