Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 每隔X秒从URL响应本机获取_React Native - Fatal编程技术网

React native 每隔X秒从URL响应本机获取

React native 每隔X秒从URL响应本机获取,react-native,React Native,在页面中,我有两件事要做,首先我从API获取一些内容并显示它。之后,我将每隔5秒获取另一个API,以查看显示内容的状态是否已更改 在myMyScreen.js中 const MyScreen = props => { const dispatch = useDispatch(); const onConfirmHandler = async () => { try { //get content from API and dis

在页面中,我有两件事要做,首先我从API获取一些内容并显示它。之后,我将每隔5秒获取另一个API,以查看显示内容的状态是否已更改

在my
MyScreen.js中

const MyScreen = props => {
    const dispatch = useDispatch();
    const onConfirmHandler = async () => {
        try {
            //get content from API and display on the screen
            const response1 = await dispatch(action1(param1,param2));
            if(response1==='success'){
                // I want to check the result of response2 every 5 seconds, how can I do this?
                const response2 = await dispatch(action2(param3));
            }
        }catch {...}
    }

    return (
        <Button title='confirm' onPress={onConfirmHandler}}>
    )
}

我也遇到过这个问题。这里不能使用超时功能。相反,您可以使用或包。我更喜欢本地背景提取。因此,您可以在特定的时间间隔获取结果,并在内容更改时更新状态。

简单的设置时间间隔就足够了。
export default action1 = (param1,param2) ={
    return async (dispatch, getState) => {
        // To call the API, I need to used token I got when login
        let = getState().login.token;
        const response = await fetch(url,body);
        const resData = await response.json();
        if (resData==='success'){
            return param3
        }
}

export default action2 = (param3) ={
    return async (dispatch, getState) => {
        // To call the API, I need to used token I got when login
        let = getState().login.token;
        const response = await fetch(url,body);
        const resData = await response.json();
        if (resData==='success'){
            // if the content status changed, I change the view in MyScreen.js
            return 'changed';
        }
}