function flatten(arr) {
var result = []
for (var i = 0; i++; i < arr.length) {
if (Array.isArray(arr[i])) {
result = result.concat(flatten(arr[i]))
} else {
result.push(arr[i])
}
}
return result;
}
function flatten(arr) {
while (arr.some((item) => Array.isarray(item))) {
arr = arr.concat(...arr)
}
return arr;
}
function flatten(arr) {
}
class EventEmitter {
constructor() {
this.cache = {}
}
on(name, fn) {
if (this.cache[name]) {
this.cache[name].push(fn)
} else {
this.cache[name] = [fn]
}
}
off(name, fn) {
let tasks = this.cache[name]
if (tasks) {
const index = tasks.findIndex(f => f === fn)
index >= 0 && tasks.splice(index, 1)
}
}
emit(name, once = false, ...args) {
if (this.cache[name]) {
let tasks = this.cache[name].slice()
for (let fn of tasks) {
debugger;
fn(...args)
}
if (once) {
delete this.cache[name]
}
}
}
}
const emitter = new EventEmitter();
function callback() {
console.log('Callback function');
emitter.on('event', callback);
}
emitter.on('event', callback);
emitter.emit('event');
function parseParam(url) {
const paramStr = /.+\?(.+)/exec(url)[1]
const paramArr = paramStr.split('&')
let paramsObj = {}
paramArr.forEach(item => {
if (/=/.test(item)) {
const [key, val] = item.split('=')
val = decodeURIComponent(val);
val = /^\d+$/.test(val) ? ParseFloat(val) : val;
if (paramsObj.hasOwnPropoty(key)) {
paramsObj = [].concat(paramsObj[key], val)
} else {
paramsObj[key] = val
}
} else {
paramsObj[item] = true;
}
})
return paramsObj;
}
function render(template, data) {
const reg = /\{\{(\w+)\}\}/;
if (reg.test(template)) {
const name = reg.exec(template)[1]
template = template.replace(reg, data[name])
return render(template, data)
}
return template;
}
let template = '我是{{name}},年龄{{age}},性别{{sex}}';
let person = {
name: '布兰',
age: 12
}
render(template, person);
let imgList = [...document.querySelectorAll('img')]
let length = imgList.length
const imgLazyLoad = (function () {
let count = 0
return function () {
let deleteIndexList = []
imgList.forEach((img, index) => {
let rect = img.getBoundingClientRect()
if (rect.top < window.innerHeight) {
img.src = img.dataset.src
deleteIndexList.push(index)
count++
if (count === length) {
document.removeEventListener('scroll', imgLazyLoad)
}
}
})
imgList = imgList.filter((img, index) => !deleteIndexList.includes(index))
}
})()
document.addEventListener('scroll', imgLazyLoad)
function debounce(func, delay) {
let timer;
return function () {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, arguments);
}, delay);
};
}
const debounce = (func, delay) => {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, delay);
};
};
function throttle(func, delay) {
let timer;
return function () {
if (!timer) {
timer = setTimeout(() => {
func.apply(this, arguments);
timer = null;
}, delay);
}
};
}
const debouncedImgLazyLoad = debounce(imgLazyLoad, 200);
document.addEventListener('scroll', debouncedImgLazyLoad);
images.forEach(item => {
item.dataset.src = item.src
item.removeAttribute('src')
imgObserver.observer(item)
})
const imgObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(item => {
if (!item.isIntersection) return
const img = item.target
const src = img.dataset.src
if (!src) return
img.setAttribute('src', src)
observer.unobserver(img)
})
})