Skip to content

Instantly share code, notes, and snippets.

@petermac-
petermac- / 1-detect-animation-end-examples.js
Last active September 12, 2015 19:14
detect-animation-end-examples.js
// jQuery example
// http://osvaldas.info/examples/detecting-css-animation-transition-end-with-javascript/oncssanimationend.js
$( '.item' ).addClass( 'disappear' ).onCSSAnimationEnd( function()
{
$( this ).remove();
});
// JavaScript example
// http://osvaldas.info/examples/detecting-css-animation-transition-end-with-javascript/jquery.oncssanimationend.js
var item = document.querySelector( '.item' );
@petermac-
petermac- / 1-matchesSelector.js
Last active September 12, 2015 19:14
matchesSelector.js
function matchesSelector(el, selector) {
var p = Element.prototype;
var f = p.matches || p.webkitMatchesSelector || p.mozMatchesSelector || p.msMatchesSelector || function(s) {
return [].indexOf.call(document.querySelectorAll(s), this) !== -1;
};
return f.call(el, selector);
}
// Usage
matchesSelector(document.getElementById('myDiv'), 'div.someSelector[some-attribute=true]')
@petermac-
petermac- / 1-insertRule.js
Last active September 12, 2015 19:14
insertRule.js
var sheet = (function() {
// Create the <style> tag
var style = document.createElement('style');
// Add a media (and/or media query) here if you'd like!
// style.setAttribute('media', 'screen')
// style.setAttribute('media', 'only screen and (max-width : 1024px)')
// WebKit hack :(
style.appendChild(document.createTextNode(''));
@petermac-
petermac- / 1-isNative.js
Last active September 12, 2015 19:14
isNative.js
;(function() {
// Used to resolve the internal `[[Class]]` of values
var toString = Object.prototype.toString;
// Used to resolve the decompiled source of functions
var fnToString = Function.prototype.toString;
// Used to detect host constructors (Safari > 4; really typed array specific)
var reHostCtor = /^\[object .+?Constructor\]$/;
@petermac-
petermac- / 1-debounce.js
Last active September 12, 2015 19:14
debounce.js
var debounce = function(func, wait) {
// we need to save these in the closure
var timeout, args, context, timestamp;
return function() {
// save details of latest call
context = this;
args = [].slice.call(arguments, 0);
timestamp = new Date();
@petermac-
petermac- / 1-polling.js
Last active September 12, 2015 19:14
polling.js
function poll(fn, callback, errback, timeout, interval) {
var endTime = Number(new Date()) + (timeout || 2000);
interval = interval || 100;
(function p() {
// If the condition is met, we're done!
if(fn()) {
callback();
}
// If the condition isn't met but the timeout hasn't elapsed, go again
@petermac-
petermac- / 1-polling-deferred.js
Last active September 12, 2015 19:14
polling-deferred.js
// The polling function
function poll(fn, timeout, interval) {
var dfd = new Deferred();
var endTime = Number(new Date()) + (timeout || 2000);
interval = interval || 100;
(function p() {
// If the condition is met, we're done!
if(fn()) {
dfd.resolve();
@petermac-
petermac- / 1-getAbsoluteUrl.js
Last active September 12, 2015 19:14
getAbsoluteUrl.js
var getAbsoluteUrl = (function() {
var a;
return function(url) {
if(!a) a = document.createElement('a');
a.href = url;
return a.href;
};
})();
@petermac-
petermac- / 1-once.js
Last active September 12, 2015 19:14
once.js
function once(fn, context) {
var result;
return function() {
if(fn) {
result = fn.apply(context || this, arguments);
fn = null;
}
return result;
@petermac-
petermac- / 1-poll.js
Last active September 12, 2015 19:15
poll.js
function poll(fn, callback, errback, timeout, interval) {
var endTime = Number(new Date()) + (timeout || 2000);
interval = interval || 100;
(function p() {
// If the condition is met, we're done!
if(fn()) {
callback();
}
// If the condition isn't met but the timeout hasn't elapsed, go again