我需要让这个页面自动刷新,让ajax调用后端3分钟就好了,如何设置这个?我在想的是在ajax中设置一个间隔,让它每隔3分钟自动调用一次?有没有办法实现这个? 或者有更好的解决方案来实现这个?
export default class InventoryLevelReport extends React.Component {
constructor() {
super();
this.state = {
data: [
{
sku: null,
inventoryCount: 0
}
],
url: '/mft/api/reports/inventory-view'
};
}
sortByCount() {
this.setState({data: _.orderBy(this.state.data, (i) => i.inventoryCount)});
}
componentDidMount() {
ajax(this.state.url, {
success: data => {
console.log(data);
this.setState({data: data.data});
}
});
}
render() {
const buttonStyle = {
position: 'relative',
float: 'right'
};
return <div className="content">
<Button style={buttonStyle} onClick={() => this.sortByCount()}>Sort</Button>
<div>
<Table tableType='bordered'>
<th>SKU</th>
<th>Count</th>
<tbody>
{this.state.data.map((data, i) => <InventoryTableRow data={data} key={i} />)}
</tbody>
</Table>
</div>
</div>;
}
}
class InventoryTableRow extends React.Component {
render() {
return (
<TableRow>
<TableColumn>{JSON.stringify(this.props.data.sku).split('"').join('')}</TableColumn>
<TableColumn>{JSON.stringify(this.props.data.inventoryCount)}</TableColumn>
</TableRow>
);
}
}
答案 0 :(得分:2)
您应该可以使用setInterval
打包ajax电话constructor() {
super();
// ... existing constructor code
this.loadData = this.loadData.bind(this)
}
loadData() {
ajax(this.state.url, {
success: data => {
console.log(data);
this.setState({data: data.data});
}
}
}
componentDidMount() {
this.loadData()
setInterval(this.loadData), 180000); // 3 minutes in milliseconds
}