一个函数节流提高性能:
var canRun = true;
document.getElementById("throttle").onscroll = function(){
if(!canRun){
// 判断是否已空闲,如果在执行中,则直接return
return;
}
canRun = false;
setTimeout(function(){
// 这里加载图片
console.log("函数节流");
canRun = true;
}, 500);
};
延迟问题:
function delay(timer) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve();
}, timer)
})
}
使用promise来完成对settimeout的延迟,要与sleep等休眠以及其他多线程完成区分
用CSS创建一个三角形的原理是分别设置上下左右的border属性,中间内容为0面积。实现:
width: 0;
height: 0;
border-left: 50px transparent solid;
border-right: 50px transparent solid;
border-top: 50px transparent solid;
border-bottom: 50px black solid;
}```