Skip to content

Instantly share code, notes, and snippets.

View davidgarsan's full-sized avatar
💩
entropy

David davidgarsan

💩
entropy
View GitHub Profile
@davidgarsan
davidgarsan / DoEachSecond
Created July 23, 2013 14:12
Do something each second using requestAnimationFrame instead of setTimeout or setInterval.
var time = new Date().getTime();
var interval = 1000;
(function update() {
requestAnimationFrame(update);
var now = new Date().getTime(),
delta = now - (time || now);
if(delta>=interval) {
// Update a "time ago"|"time left" or do something...
console.log((new Date(now)).getSeconds());
package com.emil.android.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
/**
* Check device's network connectivity and speed
* @author emil http://stackoverflow.com/users/220710/emil

Keybase proof

I hereby claim:

  • I am davidgarsan on github.
  • I am davidgarsan (https://keybase.io/davidgarsan) on keybase.
  • I have a public key whose fingerprint is DD0B 6620 AB30 D2CB 1E47 7206 F7B7 2F6E 08CC 0955

To claim this, I am signing this object:

@davidgarsan
davidgarsan / .gitlab-ci.yml
Created October 16, 2016 08:34 — forked from b0bbywan/.gitlab-ci.yml
Build and Deploy a javascript app with Gitlab-Ci
image: node:argon
before_script:
- apt-get -qq update
- apt-get -qq install -y python2.7 python2.7-dev build-essential make gcc g++ libicu-dev
- npm -g install npm --silent
- "echo -e \"export default {CLIENT_ID: '$CLIENT_ID'}\" > app/scripts/settings.js"
- npm set progress=false
- npm install --silent
@davidgarsan
davidgarsan / IE_base_bug.html
Created July 14, 2017 06:40
Fix for IE8 and IE9 base tag bug
<head>
<base href="/">
<!--[if IE]><script type="text/javascript">
// Fix for IE ignoring relative base tags.
// See http://stackoverflow.com/questions/3926197/html-base-tag-and-local-folder-path-with-internet-explorer
(function() {
var baseTag = document.getElementsByTagName('base')[0];
baseTag.href = baseTag.href;
})();
</script><![endif]-->
/**
* @desc Returns a template function builder.
* @param template - template string.
* @param params - Substitution params.
* @return {function} - Template function.
*
* @example
* var template = buildTemplate("Hello, my name is ${params}");
* template('David'); // Hello, my name is David
*
var xhr = function() {
var xhr = new XMLHttpRequest();
var mapper = function(data) {
return Object.keys(data).map(function(k) {
return encodeURIComponent(k) + '=' + encodeURIComponent(data[k])
}).join('&');
};
return function(method, url, data, success, error, isJSON, headers) {
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
function spyObjectProperty(obj, key) {
var val = obj[key];
Object.defineProperty(obj, key, {
get() {
return val;
},
set(newVal) {
val = newVal;
console.log(key + ' is now ' + val);
function attributeChangeDetector(element, callback) {
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type == 'attributes') {
console.log('Attribute ' + mutation.attributeName + ' changed');
!!callback && callback(mutation);
}
});
var memoizeNewValue = function() {
var old = null;
return function(newVal){
if(newVal === old) {
return false;
} else {
old = newVal;
return true;
}
}