Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Need Help]在轮询中如何更好地调用cancel函数 #198

Closed
CS-liujf opened this issue Jun 1, 2023 · 3 comments
Closed

[Need Help]在轮询中如何更好地调用cancel函数 #198

CS-liujf opened this issue Jun 1, 2023 · 3 comments
Labels
2.x 2.x bug Something isn't working question Further information is requested

Comments

@CS-liujf
Copy link

CS-liujf commented Jun 1, 2023

问题描述 Problem Description

我使用了轮询功能,想要的效果是当轮询得到的数据满足某个要求时就停止轮询,所以我在Options.onSuccess中判断得到的数据,然后适时调用cancel函数,如下面的代码所示

const {data, run, loading, cancel } = useRequest(getQrStatusReq, {
  pollingInterval : 2000,
  manual: true,
  onError:(error)=>{console.log(error)},
  onSuccess:(res)=>{
     if(res.data === 1){
        cancel()
        login()
     }
  }
});

但这样行不通,它并不能使轮询停止。这是我的第一个疑问,为什么不能?
于是我就换了一个方法,在watch函数中监测data,如果条件满足则调用cancel函数,这个方法可以达到要求,不过我想知道这个方法是否符合规范。因此,我的第二个疑问是在哪里去调用cancel函数才是符合规范的?

@CS-liujf CS-liujf added the question Further information is requested label Jun 1, 2023
@John60676 John60676 added bug Something isn't working 2.x 2.x labels Jun 1, 2023
@John60676
Copy link
Member

已经修复了,onSuccessonError 应该能取消轮询才是正确的。待会会发个版

@CS-liujf
Copy link
Author

CS-liujf commented Jun 1, 2023

已经修复了,onSuccessonError 应该能取消轮询才是正确的。待会会发个版

原来如此,没想到是一个bug,等你发新版后我再试试

@John60676
Copy link
Member

已更新2.x正式版,请使用 npm install vue-requst 或者 yarn install vue-requst 更新

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.x 2.x bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants