Skip to content

Instantly share code, notes, and snippets.

function debounce(fn, delay) {
var timeout;
return function() {
var _this = this;
var _arguements = arguments;
clearTimeout(timeout);
timeout = setTimeout(function() {
fn.apply(_this, _arguements);
}, delay);
@langjt
langjt / setTimeout.js
Last active July 3, 2018 09:52
宏微事件
setTimeout(function() {
console.log(1)
}, 0);
new Promise(function(a, b) {
console.log(2);
for(var i = 0; i < 1000000; i++) {
i == 999999 && a();
}
console.log(3);
}).then(function() {
@langjt
langjt / sleep.js
Last active May 30, 2018 20:42
sleep in js
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
const start = +new Date;
console.log('Dude!');
@langjt
langjt / Closure.js
Last active July 3, 2018 09:52
Closure
var createPet = function(name) {
var sex;
return {
setName: function(newName) {
name = newName;
},
getName: function() {
return name;
@langjt
langjt / getPrototypeChain.js
Last active July 3, 2018 09:53
getPrototypeChain
function getPrototypeChain(object) {
var protoChain = [];
while (object = object.__proto__) {
protoChain.push(object);
}
protoChain.push(null);
return protoChain;
}
@langjt
langjt / eventLoop.js
Last active July 3, 2018 09:53
事件循环
function printing() {
console.log(1);
setTimeout(function() { console.log(2); }, 1000);
setTimeout(function() { console.log(3); }, 0);
console.log(4);
}
@langjt
langjt / lang.extend.js
Last active July 3, 2018 09:53
sample extend
var lang = function(){};
lang.extend = lang.prototype.extend = function(){
var len = arguments.length,
target = {},
options, name, src, copy, clone, i= 0;
if (len === 1) {
target = this;
} else if(len > 1) {
function getElementsByClassName(clsName, element) {
var arr = [];
var aEle = element.getElementsByTagName('*');
var re = new RegExp('\\b'+ clsName +'\\b');
for (var i= 0,len=aEle.length; i<len; i++) {
if (re.test(aEle[i].className)) {
arr.push(aEle[i]);
/* 有一个数n,不用for/while循环,怎么返回[1,2,3,...,n]这样一个数组 */
// 方法一:递归
function recursionLoop(n) {
var arr = [];
return (function () {
arr.unshift(n); // 入队
n--;
if (n != 0) {
/* a,b两个变量,不用第三个变量来交换两个变量的值 */
// a,b为数字
var a = 1,
b = 2;
a = a + b;
b = a - b;
a = a - b;