Skip to content

Instantly share code, notes, and snippets.

@justinobney
justinobney / when_condition_met.js
Created October 17, 2012 13:12
Poll until a condition is met
function when_condition_met(fn_condition, fn_callback, interval, opt_max_attempt) {
var poll_id;
var run_count = 0;
poll_id = setInterval(function () {
if (typeof opt_max_attempt == "number" && run_count == opt_max_attempt) {
clearInterval(poll_id);
return; // possible run onfail callback.. If so refactor to be onject based params.
}
@justinobney
justinobney / event_delegate_logging.js
Created January 21, 2013 13:31
Patch handlers to log what they are doing..
function patchDelegate(delegate){
console.log("Patching: ", delegate.type, delegate.selector);
var type = delegate.type;
var handler = delegate.handler;
var selector = delegate.selector;
var newHandler = function(e){
e.preventDefault();
console.group();
console.log("Event triggered for: " + selector);
function DetectOS() {
var osName = "Unknown OS";
var platform = navigator.platform.toUpperCase();
if (platform.indexOf("WIN") != -1) osName = "Windows";
if (platform.indexOf('MAC') != -1) osName = "MacOS";
if (platform.indexOf("X11") != -1) osName = "UNIX";
if (platform.indexOf("LINUX") != -1) osName = "Linux";
return osName;
@justinobney
justinobney / jQuery.serializeObject.js
Created March 1, 2013 16:57
jQuery serializeObject
(function($){
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
@justinobney
justinobney / gravatar.js
Created March 2, 2013 16:49
MD5 Hash needed for gravatar
/**
*
* MD5 (Message-Digest Algorithm)
* http://www.webtoolkit.info/
*
**/
var MD5 = function (string) {
function RotateLeft(lValue, iShiftBits) {
@justinobney
justinobney / js_closure_begginner.js
Last active December 15, 2015 10:59
JS Closure Beginner Example
var MyApp = MyApp || {};
(function(app) {
var count = 0;
app.add = add;
app.getCount = getCount;
function add() {
count++;
@justinobney
justinobney / promise_working_bar.js
Created March 28, 2013 03:57
Web App Working Bar - Like GMail
var WorkerBar = (function(){
var promises = [];
var workingbar = $('.working');
var done;
var add = function(promise) {
if ( promises.length == 0 ){
showWorking();
}
@justinobney
justinobney / jquery.wait.js
Created March 28, 2013 14:45
A more expressive setTimeout
$.wait = function(time) {
return $.Deferred(function(dfd) {
setTimeout(dfd.resolve, time);
}).promise();
};
/*
----------------------------
Use Case
----------------------------
@justinobney
justinobney / promise_bag.js
Last active December 15, 2015 13:39
Promise Bag - Send promises in and monitor begin and duration while promises are being resolved & all promises resolved.
/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, strict:true, undef:true, unused:true, browser:true, devel:true, jquery:true, indent:4, maxerr:50, node:true */
/* global _ */
(function ($, _) {
"use strict";
//Actual app logic
var PromiseCue = function (options) {
var fn = function () {},
@justinobney
justinobney / Countdown.js
Created April 10, 2013 15:25
* This provides a countdown with events that can be listened for * Events: done, tick ( or progress ) * Methods: init, reset
var Countdown = function ($, undefined) {
'use strict';
var interval,
initialMinutes = 5,
initialSeconds = 0,
dfd = $.Deferred();
var vars = {
isInitialized: false,