函数节流(throttle),指的是某个函数在必定时间间隔内(例如 3 秒)只执行一次,在这 3 秒内产生函数调用请求直接无视,也不会延长时间间隔。3 秒间隔结束后第一次遇到新的函数调用会触发执行,而后在这新的 3 秒内依旧无视新的函数调用请求,以此类推。react
快速记忆:安安静静地作好这一件事,无论你怎么打扰。浏览器
被频繁调用的场景。若是有大量计算,频繁操做 DOM,资源加载等行为,可能会致使 UI 卡顿(线程阻塞),严重会致使浏览器奔溃。app
resize
、scroll
等事件;mousemove
事件;// 0.1.1/throttle.js
/** * * @param {Function} callback 回调函数 * @param {Number} wait 间隔时间 * * @return {Function} 节流函数 */
function throttle(callback, wait = 3000) {
let timer = null;
let startTime;
return function () {
const ctx = this;
const args = arguments;
const now = +new Date();
if (startTime && now < startTime + wait) {
clearTimeout(timer);
timer = setTimeout(function () {
startTime = now;
callback.apply(ctx, args);
}, wait);
} else {
startTime = now;
callback.apply(ctx, args);
}
}
}
复制代码
// 0.1.1/throttle.page.js
const body = document.querySelector('body');
const btn = document.createElement('div');
btn.style = 'cursor: pointer; padding: 10px; background:red; border-radius: 5px; text-align: center; color:#fff;';
btn.innerHTML = '函数节流默认 3 秒';
body.append(btn);
function callback() {
console.log('pr');
}
btn.addEventListener('click', throttle(callback));
复制代码
import React from 'react';
import { throttle } from './throttle';
// 使用
export default class ThrottleInReact extends React.Component {
constructor() {
super();
this.change = throttle(e => {
console.log(e.target.value);
}, 1000);
}
onWindowResize = () => {
console.log('resize');
}
onRemoveWindowResize = () => {
console.log('remove resize');
}
handleChange = e => {
e.persist();
this.change(e);
}
render() {
return (
<input onChange={this.handleChange} />
)
}
componentDidMount() {
window.addEventListener('resize', throttle(this.onWindowResize, 60));
}
componentWillUnmount() {
window.removeEventListener('resize', throttle(this.onRemoveWindowResize, 60));
}
}
复制代码