简体   繁体   English

如何防止多次ajax查询在多次单击“prev”按钮上运行

[英]How can I prevent multiple ajax queries from being run on “prev” button being clicked multiple times

I have my calendar on "agendaWeek" view by default. 我默认在“agendaWeek”视图中显示我的日历。 I'm using "events (as a function)" in order to pull in dynamic event data. 我正在使用“事件(作为一个函数)”来引入动态事件数据。

In order to quickly get to a week I'd like to view, I click multiple times on "prev" quickly. 为了快速到达我想查看的一周,我快速点击“prev”多次。 The interface catches up quickly, but each click triggers an ajax request. 界面快速赶上,但每次单击都会触发ajax请求。 This is not ideal because it slows the response time right down for the desired week, as each intervening week's ajax request must be processed. 这并不理想,因为它会缩短所需周的响应时间,因为必须处理每个干预周的ajax请求。

How can this be alleviated? 怎么能减轻这种情况? My thinking is that this is a feature request really. 我的想法是,这确实是一个功能请求。 The library should wait say 300 milliseconds before executing "event" function. 在执行“event”功能之前,库应该等待300毫秒。

It sounds like you need to debounce the ajaxing function: 听起来你需要去抖动ajaxing函数:

The Debounce technique allow us to "group" multiple sequential calls in a single one. Debounce技术允许我们在一个中“分组”多个顺序调用。

Here's the latest lodash implementation, which is what I'd use. 这是最新的lodash实现,这是我使用的。

function delayAjax(ajaxCall){
    clearTimeout(myFn);

    myFn = setTimeout(ajaxCall(), 300)

}

and

<button id="btnNext" onclick="delayAjax(ajaxCall);"><button>

something like that... 类似的东西......

I was able to get the debounce to work by using the eventSources and returning an array of functions, one for each event source. 通过使用eventSources并返回一个函数数组,每个事件源都有一个函数,我能够使eventSources工作。 The getSources function is simply for convenience. getSources函数只是为了方便起见。 In the fullCalendar configuration we would have: 在fullCalendar配置中,我们将:

...
eventSources: getSources(),
...

And the functions code: 功能代码:

let source1Params = {};
let source2Params = {};

let debouncedSource1 = debounce(
  function () {
    $.ajax( {
      url: 'source/1/feed',
      dataType: 'json',
      data: {
        start: source1Params.start,
        end: source1Params.end
      },
      success: function( response ) {
        // Parse the response into an event list
        let eventList = ...
        source1Params.callback( eventList );
      }
    } );
  }, 200
);

let debouncedSource2 = debounce(
  function () {
    $.ajax( {
      url: 'source/2/feed',
      dataType: 'json',
      data: {
        start: source2Params.start,
        end: source2Params.end
      },
      success: function( response ) {
        // Parse the response into an event list
        let eventList = ...
        source2Params.callback( eventList );
      }
    } );
  }, 200
);

function getSources() {
  return [
    {
      id: 'source1',
      events: ( start, end, timezone, callback ) => {
        source1Params = {
          'start': start,
          'end': end,
          'timezone': timezone,
          'callback': callback
        };
        debouncedSource1();
      }
    },
    {
      id: 'source2',
      events: ( start, end, timezone, callback ) => {
        source2Params = {
          'start': start,
          'end': end,
          'timezone': timezone,
          'callback': callback
        };
        debouncedPlanned();
      }
    }
  ];
}

This works partially, ie if you click the next or prev buttons consecutively, it will prevent the multiple requests, sending only the last one as desired. 这部分工作,即如果您连续单击下一个或上一个按钮,它将阻止多个请求,根据需要仅发送最后一个请求。 However this also has multiple caveats: 然而,这也有多个警告:

  • The loading callback in the configuration will no longer work. 配置中的loading回调将不再起作用。 This is because how the loadingLevel is implemented internally, it will be increased by the fast clicking, but not reduced, hence never reaching zero, that triggers the callback. 这是因为loadingLevel是如何在内部实现的,它会通过快速点击而增加,但不会减少,因此永远不会达到零,从而触发回调。
  • Clicking next, waiting for the debounce timer to end and the load to start, and clicking next again while it's loading will result in unpredictable results, with events not being rendered. 单击下一步,等待去抖定时器结束并启动加载,然后在加载时再次单击下一步将导致不可预测的结果,事件未呈现。 My solution right now is disabling the navigation buttons while events are loading. 我现在的解决方案是在加载事件时禁用导航按钮。 This fixes the problem but makes navigation less responsive. 这解决了问题但导致导航响应性降低。

user observables it will help you solving your problem observable 用户可观察它将帮助您解决您的问题可观察性

also read this article http://blog.thoughtram.io/angular/2016/01/06/taking-advantage-of-observables-in-angular2.html 另请阅读这篇文章http://blog.thoughtram.io/angular/2016/01/06/taking-advantage-of-observables-in-angular2.html

this code is from link above its true its in angular but you can use observables everywhere. 这段代码来自上面的链接,它的真实角度,但你可以在任何地方使用observables。

constructor(private wikipediaService: WikipediaService) {
    this.term.valueChanges
             .debounceTime(400)
             .subscribe(term => this.wikipediaService.search(term).then(items => this.items = items));
  }

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

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