简体   繁体   English

处理多个 ajax 请求 - 仅使用最后一个请求

[英]Handling multiple ajax requests - use only last request

We have a calendar application where the user can click the right or left arrow in order to move to the next or previous month.我们有一个日历应用程序,用户可以在其中单击右箭头或左箭头以移动到下一个月或上个月。 A click on one of the arrows makes a jquery ajax call to load and draw the relevant month info (entire process takes about one second).单击其中一个箭头会调用 jquery ajax 来加载和绘制相关的月份信息(整个过程大约需要一秒钟)。

If a user needs to move five or six months in one direction, they usually click on the arrow very quickly thus making multiple ajax calls.如果用户需要在一个方向移动五六个月,他们通常会很快点击箭头,从而进行多次 ajax 调用。

What is the best way to 'drop' all previous calls if their processing hasnt completed and handle only the latest call?如果他们的处理尚未完成并且只处理最新的呼叫,那么“放弃”所有先前呼叫的最佳方式是什么?

For example if the calendar is showing January and the user clicks five times quickly to show June, processing (client side) for February - May should be avoided.例如,如果日历显示一月并且用户快速单击五次以显示六月,则应避免处理(客户端)二月 - 五月。

One possibility would be for each API call to be routed through a Promise.race , which uses the API call and another constructed Promise that rejects if another API call is made before the current call resolves.一种可能性是通过Promise.race路由每个 API 调用,该调用使用 API 调用和另一个构造的 Promise,如果在当前调用解析之前进行了另一个 API 调用,则该Promise将拒绝。 For example:例如:

 // API call resolves after 800 ms: const apiCall = () => new Promise(res => setTimeout(res, 800, 'foobar')); let lastReject; function doCall() { if (lastReject) lastReject(); Promise.race([ apiCall(), new Promise((_, rej) => { lastReject = rej; }) ]) .then((resp) => { console.log('API response: ' + resp); }) .catch(() => { console.log('Quick click: previous ongoing API call will be ignored'); }); } document.querySelector('input').onclick = doCall;
 <input type="button" value="Click to make API call">

You should have a variable storing the last value (month).您应该有一个变量来存储最后一个值(月)。 This variable should be set BEFORE you start your Ajax (first thing in click eventhandler).Then, when Ajax returns, you check if your Ajax call is Indeed the last value.应该在启动 Ajax 之前设置此变量(单击事件处理程序中的第一件事)。然后,当 Ajax 返回时,检查您的 Ajax 调用是否确实是最后一个值。 Your Ajax call should return the month as well as other response, so you can compare with your variable.您的 Ajax 调用应该返回月份以及其他响应,以便您可以与变量进行比较。

I had a similar issue... what I did was implement a debounce function, here's a snippet of my code... hopefully this helps我有一个类似的问题......我所做的是实现一个去抖动功能,这是我的代码片段......希望这会有所帮助

  genJobOccurrences() {
    //this doesn't call the function until the user stops clicking for >= 600ms
    this.debounce(this.jobOccurrencesAPICalls, 600)(payload);
  },

  async jobOccurrencesAPICalls(payload: OccurrencePayload) {
     //api call
      await this.fetchCalendarEvents(payload);
  }

this is my debounce function这是我的去抖功能

 debounce(fn: Function, delay = 1000) {
  // if should this only be within the scope of the function? let timer = 0 else use a global timer
  if (this.timer) clearTimeout(this.timer);

  return (...args: any[]) => {
    this.timer = setTimeout(() => {
      fn(...args);
    }, delay);
  };
}

hope this helps :)希望这可以帮助 :)

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

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