- 函数节流是什么?
函数节流的思想:某些代码不可以在没有间断的情况下连续重复执行。
3. 实践
function throttle(method,context){
clearTimeout(this.tId);
this.tId = setTimeout(function(){
method.call(context);
},100)
}
function resizeDiv(event){
var root = document.getElementById('root');
root.style.height = root.offsetWidth + 'px';
}
window.onresize = function(event){
throttle(resizeDiv);
}
额外知识:
- offsetWidth 水平方向+左右padding+左右border-width
- offsetHeight 垂直方向+上下padding+上下border-width