Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

曾浩 think2011

🏠
Working from home
View GitHub Profile
@think2011
think2011 / Watermark.js
Created Apr 11, 2015
给图片加水印
View Watermark.js
/**
* 给图片打文字水印
* @param img Object||String
* @param opt
* @constructor
*/
function Watermark(img, opt) {
this.opt = opt;
View promise-retry.js
function retry(func, interval = 1000, times = 6) {
return function retryTask(...args) {
let innerTimes = times;
return new Promise(async function loop(resolve, reject) {
innerTimes -= 1;
try {
const res = await func.apply(this, args);
resolve(res);
View cookies.js
// 提取cookie
((cookies) => {
window.COOKIES = cookies.reduce((cur, next) => {
const [k, v] = next.split('=')
cur[k] = v
return cur
}, {})
})(document.cookie.split('; '))
@think2011
think2011 / cloneObj.js
Created May 12, 2017
cloneObj 深拷贝
View cloneObj.js
var cloneObj = function(obj){
var str, newobj = obj.constructor === Array ? [] : {};
if(typeof obj !== 'object'){
return;
} else if(window.JSON){
str = JSON.stringify(obj), //系列化对象
newobj = JSON.parse(str); //还原
} else {
for(var i in obj){
newobj[i] = typeof obj[i] === 'object' ?
@think2011
think2011 / swap.js
Created May 9, 2017
交换数组的位置
View swap.js
/**
* 交换数组的位置
* @param arr
* @param i {Number} 旧的位置
* @param j {Number} 新的位置
* @returns {*}
*/
function swap(arr, i, j) {
let tmp = arr[i]
View scriptLoader.js
/**
* 加载script
* @param src
* @returns {Promise}
*/
function scriptLoader(src) {
return new Promise((resolve, reject) => {
let script = document.createElement('script')
script.src = src
script.onload = resolve
@think2011
think2011 / centos-timezone.md
Last active Apr 16, 2017
更改 centos 时区
View centos-timezone.md
  • yum install ntp //安装ntp服务
  • systemctl enable ntpd //开机启动服务
  • systemctl start ntpd //启动服务
  • timedatectl set-timezone Asia/Shanghai //更改时区
  • timedatectl set-ntp yes //启用ntp同步
  • ntpq -p //同步时间
  • date -R // 查看结果
View sleep.js
const sleep = (n) => {
let start = Date.now()
while(true) if (Date.now() - start > n) break
}
@think2011
think2011 / osx-scroller.css
Last active Apr 7, 2017
os x style scroller
View osx-scroller.css
::-webkit-scrollbar {
width: 6px;
height: 8px;
}
::-webkit-scrollbar-track {
-webkit-border-radius: 5px;
border-radius: 5px;
background: rgba(0, 0, 0, 0.1);
}
View promise-reeuce.js
var guid = 0;
function run() {
guid++;
var id = guid;
return new Promise(resolve => {
// resolve in a random amount of time
setTimeout(function () {
console.log(id);
resolve(id);
}, (Math.random() * 1.5 | 0) * 1000);