This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Run promise with a max number of concurrent tasks | |
* @param {Number} limit | |
*/ | |
function promiseAllWithLimit(limit) { | |
let count = 0; | |
let queue = []; | |
const run = (fn, resolve, reject, ...args) => { | |
count++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function throttle(fn, rate) { | |
let oldTime; | |
let timer; | |
function helper(...args) { | |
const now = Date.now(); | |
// first time: need to call fn once | |
if(!oldTime || now - oldTime > rate) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function debounce(fn, delay) { | |
let timer; | |
function helper(...args) { | |
clearTimeout(timer); | |
timer = setTimeout(() => { | |
fn.apply(this, args); // keep the original context | |
}, delay); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// requestAnimationFrame Polyfill for IE9 | |
(function() { | |
var lastTime = 0; | |
var vendors = ['ms', 'moz', 'webkit', 'o']; | |
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { | |
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; | |
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] | |
|| window[vendors[x]+'CancelRequestAnimationFrame']; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get Local IP Address | |
* | |
* @returns Promise Object | |
* | |
* getLocalIP().then((ipAddr) => { | |
* console.log(ipAddr); // 192.168.0.122 | |
* }); | |
*/ | |
function getLocalIP() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* var big1 = new BigNum('234234234234234'); | |
* var big2 = new BigNum('234234234234234'); | |
* big1.multiplyBy(big2).toString(); // will output "54865676487297999188377566756" instead of 5.4865676487298e+28 | |
*/ | |
var BigNum = function(num) { | |
if (typeof num === 'string') { | |
this.parts = []; | |
while (num.length) { |