Skip to content

Instantly share code, notes, and snippets.

View gregorypratt's full-sized avatar
🤓
Coding...

Greg gregorypratt

🤓
Coding...
View GitHub Profile
@gregorypratt
gregorypratt / debounce.ts
Last active October 19, 2018 11:01
debounce function in TypeScript based on David Walsh's blog - https://davidwalsh.name/javascript-debounce-function
export default (func: Function, wait: number = 100, immediate?: boolean) => {
let timeout;
return (...args: any[]) => {
const later = () => {
timeout = null;
if (!immediate) func(...args);
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
class HelloWorld extends HTMLElement {
constructor() {
super();
console.log('constructor');
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.innerHTML = `
<h2>Example heading (scoped style)</h2>
<slot name="first"></slot>
<slot name="second"></slot>
@gregorypratt
gregorypratt / uuid.js
Created December 30, 2014 20:57
Create a UUID based on timestramp to aid uniqueness
function generateUUID(){
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
};
@gregorypratt
gregorypratt / pin.js
Last active December 15, 2015 06:09
Use JavaScript to create a simple random (but not unique!) 5 length character set between 10000 and zzzzz, useful for pins or shortened URLs etc.
var pin = (Math.floor(Math.random() * 60466175) + 1679615).toString(36);