Throttle
是一个接受函数并返回限制函数的函数。节流函数在一个窗口中只调用原始函数一次
毫秒。
多次调用throttle将返回多个throttle函数,您可以调用这些函数,并且每个函数都是时间窗口中唯一的调用。
export default class ServicesAndSolutionImg extends React.PureComponent {
static propTypes = {
src: PropTypes.string.isRequired,
alt: PropTypes.string.isRequired,
onDigitalSolution: PropTypes.func.isRequired,
onServices: PropTypes.func.isRequired,
onHosting: PropTypes.func.isRequired,
onAddons: PropTypes.func.isRequired,
};
// create the throttled function
throttleFn = throttle((e) => this.props[e.target.value], 1000, { leading: false, trailing: true })
render() {
// no need to omit anything - you know what you want
const { src, alt } = this.props;
return (
<div css={css`position: relative`}>
<Img fluid src={src} alt={alt} className="w-100 pt-3 pl-5 pr-5" />
<div css={css`
position: absolute;
top: 0;
right: 0;
left: 0;
right: 0;
width: 100%;
height: 100%;
`}>
<div css={css`
position: relative;
width: inherit;
height: inherit;
button {
cursor: pointer;
position: absolute;
top: 23%;
height: 51%;
opacity: 0;
}
button:nth-child(1) {
left: 15%;
width: 16%;
}
button:nth-child(2) {
left: 32%;
width: 16%;
}
button:nth-child(3) {
left: 48%;
width: 16%;
}
button:nth-child(4) {
left: 65%;
width: 16%;
}
`}>
<button onClick={this.throttleFn} value="onDigitalSolution" />
<button onClick={this.throttleFn} value="onServices" />
<button onClick={this.throttleFn} value="onHosting" />
<button onClick={this.throttleFn} value="onAddons" />
</div>
</div>
</div>
);
}
}