这是电子应用程序的一部分,在用户界面上按下刷新按钮时调用此操作。我想让它自动刷新。我该怎么做?
组件/counter.js:
export default class Counter extends Component<Props> {
props: Props;
render() {
const {
refresh,
counter
} = this.props;
return (
<button onClick={() => refresh()}>
Refresh
</button>
);
}
}
actions/counter.js:
export function refresh() {
// Do some local CRUD here.
return {
type: NO-OP
};
}
最佳答案
您可以创建一个连续调用 setTimeout
并在组件卸载时停止的函数:
示例
class Counter extends Component {
runRefresh = () => {
this.timer = setTimeout(() => {
this.props.refresh();
this.runRefresh();
}, 1000);
};
componentDidMount() {
this.runRefresh();
}
componentWillUnmount() {
clearTimeout(this.timer);
}
render() {
const { refresh, counter } = this.props;
return <button onClick={() => refresh()}>Refresh</button>;
}
}
关于javascript - 如何将此刷新操作称为连续后台作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51047200/