Skip to content

Instantly share code, notes, and snippets.

View dustintheweb's full-sized avatar
🎯
Focusing

Dustin Hoffmann dustintheweb

🎯
Focusing
View GitHub Profile
@dustintheweb
dustintheweb / self-clearing-setinterval-with-callback.js
Created April 11, 2015 03:00
A self clearing setInterval with a callback
var i = 0;
var timer = setInterval(function(){
// stuff
if(i === 5) {
clearInterval(timer);
console.log('callback'); // callback after some time
}
}, 200);
@dustintheweb
dustintheweb / debug-ajax-console.js
Created April 13, 2015 12:03
Javascript to debug an ajax request
var oXhr;
oXhr = new XMLHttpRequest();
oXhr.id = (new Date()).getTime();
oXhr.onreadystatechange = function() {
if (oXhr.readyState === 4 && (oXhr.status === 200)) {
// code stuffs
console.log(this.id);
}
@dustintheweb
dustintheweb / optimize-typekit-async-load-with-callback
Last active August 29, 2015 14:20
Optimize the Typekit load w/ Async & add a callback
<!-- tk optimize // http://goo.gl/2A1IF7 -->
<script type = "text/javascript" > (function(e) {
var t = 3e3;
if (window.sessionStorage) {
if (sessionStorage.getItem("useTypekit") === "false") {
t = 0;
}
}
var n = {
kitId: "abcd1234",
@dustintheweb
dustintheweb / scroll-direction.js
Created October 13, 2018 18:56
Scroll Direction Fn
const main = {};
const updateScroll = () => {
main.winScroll = $(window).scrollTop();
if (!main.winScrollMem) {
main.winScrollMem = 0;
}
if (main.winScroll > main.winScrollMem) {
main.winScrollDir = 'down';
} else {
main.winScrollDir = 'up';
@dustintheweb
dustintheweb / in-viewport.js
Last active October 13, 2018 18:59
Elements in Viewport Fn
const main = {};
const inViewport = ($el) => {
let winTop = main.winScroll;
let winBtm = winTop + main.winHeight;
let $elTop = $el.offset().top;
let $elBtm = $elTop + $el.outerHeight();
return $elBtm > winTop && $elTop < winBtm;
}
if (inViewport($el) === true) {
@dustintheweb
dustintheweb / simple-parallax.js
Created October 13, 2018 19:13
Simple Parallax
const main = {};
const updateScroll = () => {
main.winScroll = $(window).scrollTop();
};
const parallaxLoad = () => {
const $someEl = $('.someEl');
$someEl.each((i,el) => {
let $el = $(el);