Skip to content

Instantly share code, notes, and snippets.

@ahliang85
ahliang85 / debounce_1.js
Created September 7, 2015 04:51
JavaScript - Debounce & Throttle Function
function debounce(fn, delay) {
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
}
@ahliang85
ahliang85 / once.js
Created September 7, 2015 03:55
JavaScript - Once Function
// http://davidwalsh.name/javascript-once
function once(fn, context) {
var result;
return function() {
if(fn) {
result = fn.apply(context || this, arguments);
fn = null;
}
@ahliang85
ahliang85 / XMLHttpRequest.html
Last active September 7, 2022 19:48
XMLHttpRequest
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge, chrome=1"/>
<script>
"use strict";
function submitForm(oFormElement)
{
var xhr = new XMLHttpRequest();
xhr.onload = function(){ alert (xhr.responseText); }
@ahliang85
ahliang85 / imageOptim.command
Last active August 29, 2015 14:27
Command - ImageOptim-CLI
# imageoptim --image-alpha --jpeg-mini --quit --directory ~/Sites/MyProject
# shorthand format:
# imageoptim -a -j -q -d ~/Sites/MyProject
@ahliang85
ahliang85 / modify-dom.txt
Last active May 1, 2023 21:51
Chrome Extension - Modify DOM
Background page is kind of like you own mini server - it is a completely isolated page that has nothing to do with pages a user visits. It is used for controlling the rest of extension components as it is always running in background.
Content scripts are pieces of javascript code that are getting injected into actual pages a user visits and can access and modify parent page's DOM.
So to get your extension working you need to remove
<script src="jquery.min.js"></script>
<script src="content.js"></script>
from background page, and inject jquery as a content script either through manifest:
@ahliang85
ahliang85 / move-to-gif.command
Last active August 29, 2015 14:26
Command - mov to gif
ffmpeg -i in.mov -s 600x400 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=3 > out.gif
@ahliang85
ahliang85 / rAF-2.js
Last active December 19, 2015 13:48 — forked from paulirish/rAF.js
Javascript - requestAnimationFrame
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();