简体   繁体   English

Aurelia停止HTTP请求重载

[英]Aurelia stop overload of http requests

I'm using Aurelia and have multiple promises calling my API. 我正在使用Aurelia,并且有多个Promise调用我的API。 If I continually click the button to fire the promises over and over and over again the back end will timeout. 如果我连续单击该按钮一次又一次地触发承诺,则后端将超时。 How can I stop/halt the promises I am firing and just get the newest one so as to not over work the API and cause a timeout? 我如何停止/停止我正在履行的承诺,而只是获得最新的承诺,以免API过度工作并导致超时?

This is not specific to aurelia, it happens with any asynchronous event implementation. 这不是aurelia特有的,它发生在任何异步事件实现中。

  1. When you click on a button, the event handler is called. 当您单击按钮时,将调用事件处理程序。
  2. The event handler sends an asynchronous AJAX request to your server. 事件处理程序将异步AJAX请求发送到您的服务器。 Because it's async, your code and your application continues to run while the request is happening. 因为它是异步的,所以在发生请求时,您的代码和应用程​​序将继续运行。
  3. User can click again on the button and a second AJAX request can be sent at the same time, assuming that you click faster than requests complete. 用户可以再次单击该按钮,并可以同时发送第二个AJAX请求,前提是您单击的速度比完成请求的速度快。
  4. To make it worse, concurrent requests may even complete out-of-order (ie a later request completes before a former). 更糟糕的是,并发请求甚至可能无序完成(即,后一个请求在前一个请求之前完成)。 Your code should be prepared to handle that properly. 您的代码应准备妥善处理。

If you don't want this behaviour, it is up to you to prevent the user from submitting again until the AJAX request completes. 如果您不希望这种行为,您可以自行决定阻止用户在AJAX请求完成之前再次提交。 For example you could: 例如,您可以:

  • Disable the button when sending the request; 发送请求时禁用按钮; or 要么
  • Display a modal "loading" screen / spinner to prevent any interaction with the application while the request is in flight. 显示模式“加载”屏幕/微调器,以防止在请求进行过程中与应用程序进行任何交互。

Note that giving user feedback is good UX anyway. 请注意,无论如何,给予用户反馈都是好的UX。 A network request might be delayed for a whole lot of reasons and it is a good idea to give some feedback to let him know that something is happening. 网络请求可能由于很多原因而被延迟,因此最好提供一些反馈以使他知道正在发生某些事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM