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

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

问题描述

我已阅读有关通过使用 AbortController .abort().有没有办法在调用此命令后再次启动请求而不中止请求?

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.

他们修复了示例.单击Cancel download后,您将能够开始新的下载并一遍又一遍地取消它.为了实现Download button每次都实例化一个新的 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;
  // ...

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

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

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

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