罐头博客function f<T>(arg: T): T {
return arg;
}
简单来说,Ajax 是一种思想,XMLHttpRequest 只是实现 Ajax 的一种方式
Axios 是一个基于 promise 封装的网络请求库,它是基于 XHR 进行二次封装
都是循环遍历数组中的每一项
forEach 和 map 方法里每次执行匿名函数都支持 3 个参数,参数分别是 item(当前每一项),index(索引值),arr(原数组)
匿名函数中的 this 都是指向 window
只能遍历数组
都不会改变原数组
function runWithRetry(fn, retryTimes) {
return fn()
.then((res) => {
return res;
})
.catch((err) => {
if (retryTimes === 0) {
return Promise.reject(err);
}
return runWithRetry(fn, retryTimes - 1);
});
}
function asyncApp() {
return new Promise((resolve, reject) => {
if (Math.random() > 0.7) {
resolve('success');
console.log('success');
} else {
reject('failed');
console.log('failed');
}
});
}
runWithRetry(asyncApp, 3);
function asyncApp() {
return new Promise((resolve, reject) => {
reject('failed')
resolve('success');
})
}
asyncApp().then(res => {
console.log('then1', res);
}).then(res => {
console.log(('then2', res));
}).catch(err => {
console.log('catch1', err);
}).then(res => {
console.log('then3', res);
})
// 输出
catch1 failed
then3 underfined
function CountDown({ process }) {
const [count, setCount] = useState(process);
useEffect(() => {
setCount(process);
}, [process]);
useEffect(() => {
let id = null;
if (count > 0) {
id = setTimeout(() => {
setCount(count - 1);
}, 1000);
}
return () => {
clearTimeout(id);
};
}, [count]);
return <div>{count}</div>;
}
// 同时有a, b两个class
.a.b {
color: red;
}
// a元素内的所有b子元素
.a .b {
color: red;
}
// 父级是a元素的b元素
.a > .b {
color: red;
}
// 紧跟在a元素后的第一个b元素
.a + .b {
color: red;
}