【问题标题】:Want to call api after every 5 second in react native axios api call想要在反应原生 axios api 调用中每 5 秒调用一次 api
【发布时间】:2022-01-03 08:01:37
【问题描述】:

这是我的代码:

useEffect(() => {

  var form = new FormData();

  form.append("user_id", user_id);

 axios.post('http://192.168.29.194/expense_calculator/index.php/API/get_all_transaction',form).then

((response) => {

          console.log('sss',response.data.data);

          if(response.data.data){

            setData(response.data.data);

          }

        });

      },[]);

【问题讨论】:

  • 为什么不使用套接字?

标签: reactjs react-native api react-hooks axios


【解决方案1】:

function postForm(){
  var form = new FormData();
  form.append("user_id", user_id);
  axios.post('http://192.168.29.194/expense_calculator/index.php/API/get_all_transaction',form)
    .then((response) => {
      console.log('sss',response.data.data);
      if(response.data.data){
        setData(response.data.data);
      }
  });
}

useEffect(() => {
  const timer = setInterval(() => postForm, 1000);
  return () => clearInterval(timer);
},[]);

https://developer.mozilla.org/en-US/docs/Web/API/setInterval

【讨论】:

    猜你喜欢
    • 2019-04-21
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多