每分钟请求服务器 - 其他方式然后超时?

Mal*_*nka 1 javascript reactjs

我想每分钟从服务器获取数据以检查新数据.现在我正在使用Timeout循环执行此操作,但我认为可能有更好的方法来执行此操作.应该怎么做?

async componentDidMount () {
    await this.callApi();
    await this.callNames();
}

callApi = async () => {
    try {
        const response = await fetch('/rates');
        if (response.status === 200) {
            const body = await response.json();
            this.setState({ rates: body.rates, ratesError: null });
        } else {
            this.setState({ ratesError: 'Error getting currency rates' });
        }
    } catch (err) {
        console.log(err);
        this.setState({ ratesError: err });
    }
    setTimeout(this.callApi, 60000);
}
Run Code Online (Sandbox Code Playgroud)