如何在 AbortController.abort() 之后启动另一个请求? [英] How can I start another request after AbortController.abort()?

查看:41
本文介绍了如何在 AbortController.abort() 之后启动另一个请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关使用 AbortController 取消获取请求的信息.中止().有没有办法在调用此命令后再次启动请求而不中止它?

I've read about cancelling fetch requests by using AbortController.abort(). Is there a way to start a request again without aborting it after calling this command?

例如,在 这个来自 MDN 的演示中,一次 取消下载被点击,点击下载视频会再次触发抓取,但会立即中止.

For example, in this demo from MDN, once Cancel download has been clicked, clicking Download video will trigger the fetch again, but immediately abort it.

有没有办法再次允许这个请求而不中止它?那么,在这种情况下,如何点击下载视频开始下载,点击取消下载取消下载,然后点击下载视频 再次开始下载?例如,如果用户不小心点击了取消下载...

Is there a way to allow this request again without aborting it? So, in this case, how could you click Download video to begin the download, click Cancel download to cancel the download, and then click Download video again to start the download again? For example, if the user clicked Cancel download on accident...

推荐答案

例如,在 MDN 的这个演示中,一旦点击取消下载,点击下载视频将再次触发提取,但立即中止.

For example, in this demo from MDN, once Cancel download has been clicked, clicking Download video will trigger the fetch again, but immediately abort it.

他们修复了 示例.单击取消下载后,您将能够开始新的下载并再次取消它,一遍又一遍.为了实现 Download 按钮 每次都实例化一个新的 AbortController,这样你每次都会得到一个新的中止信号:

They fixed the example. After you click Cancel download you will be able to start a new download and cancel it again, over and over. In order to achieve that the Download button instantiate a new AbortController every time, so you get a fresh signal to abort every time:

downloadBtn.addEventListener('click', fetchVideo);

function fetchVideo() {
  controller = new AbortController;
  signal = controller.signal;
  // ...

因此,可以为您可能希望取消的每个请求实例化新的 AbortController.

So it's ok to instantiate new AbortControllers for each request that you may wish to cancel.

这篇关于如何在 AbortController.abort() 之后启动另一个请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆