Skip to content

Instantly share code, notes, and snippets.

View Troland's full-sized avatar
🎯
Focusing

Tris Roland Troland

🎯
Focusing
  • China
View GitHub Profile
@Troland
Troland / performance.now()-polyfill.js
Created June 23, 2018 16:53
performance.now()-polyfill
// relies on Date.now() which has been supported everywhere modern for years.
// as Safari 6 doesn't have support for NavigationTiming, we use a Date.now() timestamp for relative values
// if you want values similar to what you'd get with real perf.now, place this towards the head of the page
// but in reality, you're just getting the delta between now() calls, so it's not terribly important where it's placed
(function(){
// prepare base perf object
if (typeof window.performance === 'undefined') {
window.performance = {};
@Troland
Troland / groupCardNumber.txt
Last active June 25, 2018 15:50
// 银行号码位数分组
// 银行号码位数分组
function groupCardNumber(cardNumber, digits) {
const re = new RegExp('\\d{'+ digits + '}', 'g');
return bankNumber.replace(re, function (match, offset, string) {
if((offset + digits - 1) !== string.length - 1) {
match = match + ' ';
}
return match;
});
@Troland
Troland / file-requestAnimationFrame.js
Created June 25, 2018 15:52
Reduce unnessary frame loss and ensure every module init when the browser is idle.
(function() {
let lastTime = 0;
const vendors = ['ms', 'moz', 'webkit', 'o'];
for(let x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = (callback, element) => {
var currTime = new Date().getTime();
@Troland
Troland / Interface-design-pattern.txt
Created June 25, 2018 15:54
Pro JavaScript Design Patterns
/**
* Code copyright Dustin Diaz and Ross Harmes, Pro JavaScript Design Patterns.
*/
// Constructor.
var Interface = function(name, methods) {
if (arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length + "arguments, but expected exactly 2.");
}
this.name = name;
@Troland
Troland / isPopWindowBlocked.txt
Created June 25, 2018 15:55
判断弹出窗口是否被浏览器拦截
function isPopWindowBlocked() {
const win = window.open(url, '', '');
return win === null;
}
@Troland
Troland / load-images.js
Created June 25, 2018 15:57
load images in parallel
const loadImage = (url, callback) => {
const image = new Image();
image.onload = () => {
callback(null, image);
};
image.onerror = () => {
callback(new Error('Could not load image at ' + url));
};
image.src = url;
}
@Troland
Troland / blobToDataURL.txt
Created June 25, 2018 15:57
Transform blob to base64 encoded string
function blobToDataURL(blob) {
return new Promise((fulfill, reject) => {
let reader = new FileReader();
reader.onerror = reject;
reader.onload = (e) => fulfill(reader.result);
reader.readAsDataURL(blob);
});
}
@Troland
Troland / urlB64ToUint8Array.txt
Created June 25, 2018 15:58
urlB64ToUint8Array
function urlB64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
@Troland
Troland / requestPermissionForWebPush.txt
Created June 25, 2018 15:58
requestPermission for web push notifications
function requestPermission() {
return new Promise((resolve, reject) => {
const permissionResult = Notification.requestPermission((result) => {
// Handling deprecated version with callback.
resolve(result);
});
if (permissionResult) {
permissionResult.then(resolve, reject);
}
@Troland
Troland / ajaxError.txt
Created June 25, 2018 16:00
jquery ajax error
$.ajax({
type: 'GET',
url: {url},
success: function(data) {
alert('Success');
},
error: function(xhr, textStatus, errorThrown) {
if (xhr.readyState == 4) {
// HTTP error (can be checked by XMLHttpRequest.status and XMLHttpRequest.statusText)
} else if (xhr.readyState == 0) {