React-Native的睡眠功能?

6 javascript android google-maps reactjs react-native

因此,我试图通过Google Places API获取React Native中的某些位置来获取所有"位置".问题是,在第一次调用API之后,Google只返回20个条目,然后返回a next_page_token,以附加到同一API调用url.因此,我提出另一个请求,以便立即获得接下来的20个位置,但是在令牌实际变为有效之前会有一个小的延迟(1-3秒),因此我的请求出错.

我试过做:

this.setTimeout(() => {this.setState({timePassed: true})}, 3000);
Run Code Online (Sandbox Code Playgroud)

但它被应用程序完全忽略了......有什么建议吗?

更新

我在我的componentWillMount函数中执行此操作(在定义变量之后),并setTimeout在此行之后调用右侧.

axios.get(baseUrl)
.then((response) => {
   this.setState({places: response.data.results, nextPageToken: response.data.next_page_token });

});
Run Code Online (Sandbox Code Playgroud)

Gui*_*zog 9

我的理解是你试图根据另一个提取的结果进行提取.所以,你的解决方案是使用TimeOut来猜测请求何时完成,然后再做另一个请求,对吗?

如果是,也许这不是解决问题的最佳方案.但是以下代码是我如何使用超时:

// Without "this"
setTimeout(someMethod,
    2000
)
Run Code Online (Sandbox Code Playgroud)

我将采取的方法是等待提取完成,然后我将使用不同的参数再次使用回调来获取相同的提取,在您的情况下,nextPageToken.我使用ES7 async和await语法执行此操作.

// Remember to add some stop condition on this recursive method.
async fetchData(nextPageToken){
    try {
        var result = await fetch(URL)
        // Do whatever you want with this result, including getting the next token or updating the UI (via setting the State)
        fetchData(result.nextPageToken)
    } catch(e){
         // Show an error message
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我误解了某些内容或者您有任何疑问,请随时提出!

我希望它有所帮助.