Skip to content

Instantly share code, notes, and snippets.

View icodeforlove's full-sized avatar
:octocat:
Indefinitely In Bangkok

Chad icodeforlove

:octocat:
Indefinitely In Bangkok
View GitHub Profile
@icodeforlove
icodeforlove / prettyTime.cpp
Last active August 29, 2015 14:03
easy way to get pretty times in c++
const char * niceTime(time_t targetTime, time_t now = time(0)) {
const char *periods[8] = {"second", "minute", "hour", "day", "week", "month", "year", "decade"};
float lengths[7] = {60.0, 60.0, 24.0, 7.0, 4.35, 12.0, 10};
int difference = now > targetTime ? now - targetTime : targetTime - now;
int i = 0;
for (; difference >= lengths[i] && i < 7; i++) {
difference /= lengths[i];
}
@icodeforlove
icodeforlove / CCTabeView.cpp
Created July 2, 2014 20:34
gives CCTableView the ability to dequeueCellWithTag, this is very useful when using multiple custom CCTableViewCell's and wanting to be able to reuse them.
CCTableViewCell *CCTableView::dequeueCellWithTag(int nTag)
{
CCTableViewCell *cell = NULL;
for (int i = 0, len = m_pCellsFreed->count(); i < len; i++) {
CCTableViewCell *tempCell = (CCTableViewCell*)m_pCellsFreed->objectAtIndex(i);
if (tempCell->getTag() == nTag) {
cell = tempCell;
cell->retain();
m_pCellsFreed->removeObjectAtIndex(i);
@icodeforlove
icodeforlove / GameViewController.swift
Created June 25, 2014 11:40
working version of GameViewController.swift for iOS 7.1
//
// GameViewController.swift
//
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
@icodeforlove
icodeforlove / dabblet.css
Created February 28, 2014 21:55
Untitled
body {
padding: 0;
margin: 0;
}
.body {
opacity: .3;
position: absolute;
left: 0;
right: 0;
top: 0;
@icodeforlove
icodeforlove / dabblet.css
Created February 19, 2014 19:55
Untitled
body {
background: #222;
font-family: 'lucida grande',tahoma,verdana,arial,sans-serif;
}
.comment-box {
border: transparent 1px solid;
height: 90px;
position: relative;
}
@icodeforlove
icodeforlove / cdn.js
Created January 2, 2014 19:19
cdn url generator
function CDN ($config) {
this.protocol = $config.protocol;
this.domain = $config.domain;
this.alias = $config.alias;
this.total = $config.total;
}
CDN.prototype = {
getURL: function (path) {
return this.protocol + '://' + this.alias + (this._stringToNumber(path) % this.total) + '.' + this.domain + path;
},
@icodeforlove
icodeforlove / date-format.js
Last active December 30, 2015 18:09
add date formatting to javascript Date objects
(function () {
function date (format, timestamp) {
// http://kevin.vanzonneveld.net
// + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
// + parts by: Peter-Paul Koch (http://www.quirksmode.org/js/beat.html)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: MeEtc (http://yass.meetcweb.com)
// + improved by: Brad Touesnard
// + improved by: Tim Wiel
// + improved by: Bryan Elliott
@icodeforlove
icodeforlove / localstoragescripts.js
Last active December 29, 2015 15:09
reduce CDN latency by storing scripts in localStorage
function getScriptsSource (scripts, checksum) {
function loadScript (path) {
var file,
hasLocalStorage = window.localStorage;
if (!hasLocalStorage || !(file = localStorage.getItem('file:' + path))) {
var request = new XMLHttpRequest();
request.open('GET', path, false);
request.send(null);
@icodeforlove
icodeforlove / btce-autorefresh.js
Last active December 29, 2015 12:09
enables autorefresh when btce is down
/*
USE:
1) copy this code
javascript:var chat=document.getElementById('nChat'),chatRect=chat.getBoundingClientRect(),interval=1000;function refresh(){var request=new XMLHttpRequest();request.open('GET','/',true);request.onreadystatechange=function(){if(request.readyState==4){var container=document.createElement('div');container.innerHTML=request.responseText;var messages=Array.prototype.slice.call(container.querySelectorAll('#nChat p.chatmessage'));messages.forEach(function(message){if(!chat.querySelector('#'+message.id)){chat.appendChild(message)}});chat.scrollTop=chat.scrollHeight-chatRect.height;setTimeout(refresh,interval)}};request.send(null)}refresh();
2) paste it into the address bar of btc-e and press return
*/
@icodeforlove
icodeforlove / AsyncTaskThrottle.js
Last active December 28, 2015 19:59
easy way to throttle async tasks
function AsyncTaskThrottle () {
this._queued = null;
this._busy = false;
}
AsyncTaskThrottle.prototype = {
do: function (task) {
if (!this._busy) {
this._queued = task;
return this._call();