Skip to content

Instantly share code, notes, and snippets.

@wochap
Last active September 13, 2017 21:29
Show Gist options
  • Save wochap/1b7916f1c3f30802973d931a0ce89d18 to your computer and use it in GitHub Desktop.
Save wochap/1b7916f1c3f30802973d931a0ce89d18 to your computer and use it in GitHub Desktop.
helpers

Format number to x digits

// source: https://stackoverflow.com/questions/8043026/javascript-format-number-to-have-2-digit
function leftPad(number, targetLength) {
    var output = number + '';
    while (output.length < targetLength) {
        output = '0' + output;
    }
    return output;
}

loadScript

function loadScript (src, callback) {
  var script = document.createElement('script');
  var hasLoaded;

  script.src = src;
  script.onreadystatechange = script.onload = function () {
      if (!hasLoaded) callback();
      hasLoaded = true;
  }

  document.getElementsByTagName('head')[0].appendChild(script);
}

Debounce

// source: https://gist.github.com/nmsdvid/8807205#file-new_gist_file-js-L6
function debounce (func, wait, immediate) {
    var timeout;
    return function() {
        var context = this, args = arguments;
        clearTimeout(timeout);
        timeout = setTimeout(function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        }, wait);
        if (immediate && !timeout) func.apply(context, args);
    };
}

Fade effect

// source: https://gist.github.com/chrisbuttery/cf34533cbb30c95ff155
function fadeIn(el, display){
  el.style.opacity = 0;
  el.style.display = display || "block";

  (function fade() {
    var val = parseFloat(el.style.opacity);
    var proceed = ((val += 0.1) > 1) ? false : true;
    
    if (proceed) {
      el.style.opacity = val;
      requestAnimationFrame(fade);
    }
  })();
}

function fadeOut(el){
  el.style.opacity = 1;

  (function fade() {
    if ((el.style.opacity -= .1) < 0) {
      el.style.display = "none";
    } else {
      requestAnimationFrame(fade);
    }
  })();
}
// source: https://stackoverflow.com/questions/15458876/check-if-a-string-is-html-or-not
function isHTML(str) {
    var a = document.createElement('div');
    a.innerHTML = str;
    for (var c = a.childNodes, i = c.length; i--; ) {
        if (c[i].nodeType == 1) return true; 
    }
    return false;
}

// source: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
// source: https://stackoverflow.com/questions/12092633/pdf-js-rendering-a-pdf-file-using-a-base64-file-source-instead-of-url
function convertDataURIToBinary (dataBase64) {
  // var base64Index = dataURI.indexOf(1) + 1.length;
  // var base64 = dataURI.substring(base64Index);
  var raw = window.atob(dataBase64);
  var rawLength = raw.length;
  var array = new Uint8Array(new ArrayBuffer(rawLength));

  for(var i = 0; i < rawLength; i++) {
    array[i] = raw.charCodeAt(i);
  }

  return array;
}

Scrolling elements

window.scrollTo(0,0)
document.querySelector('div').scrollTop = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment