Skip to content

Instantly share code, notes, and snippets.

View think2011's full-sized avatar
🏠
Working from home

曾浩 think2011

🏠
Working from home
View GitHub Profile
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);
@think2011
think2011 / cookies.js
Created September 7, 2017 20:51
提取cookie
// 提取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 10:14
cloneObj 深拷贝
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 16:44
交换数组的位置
/**
* 交换数组的位置
* @param arr
* @param i {Number} 旧的位置
* @param j {Number} 新的位置
* @returns {*}
*/
function swap(arr, i, j) {
let tmp = arr[i]
@think2011
think2011 / scriptLoader.js
Created April 18, 2017 08:01
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 April 16, 2017 06:23
更改 centos 时区
  • yum install ntp //安装ntp服务
  • systemctl enable ntpd //开机启动服务
  • systemctl start ntpd //启动服务
  • timedatectl set-timezone Asia/Shanghai //更改时区
  • timedatectl set-ntp yes //启用ntp同步
  • ntpq -p //同步时间
  • date -R // 查看结果
@think2011
think2011 / sleep.js
Created April 15, 2017 12:34
while sleep
const sleep = (n) => {
let start = Date.now()
while(true) if (Date.now() - start > n) break
}
@think2011
think2011 / osx-scroller.css
Last active April 7, 2017 09:24
os x style scroller
::-webkit-scrollbar {
width: 6px;
height: 8px;
}
::-webkit-scrollbar-track {
-webkit-border-radius: 5px;
border-radius: 5px;
background: rgba(0, 0, 0, 0.1);
}
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);
@think2011
think2011 / throttle&debounce.js
Created April 1, 2017 10:04
防抖和节流
function debounce(method, context) {
clearTimeout(method.tId);
method.tId = setTimeout(function() {
method.call(context);
}, 1000);
}
var throttle = function (action, delay){
var last = 0;
return function(){