[英]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:
然而,这也有多个警告:
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
是如何在内部实现的,它会通过快速点击而增加,但不会减少,因此永远不会达到零,从而触发回调。 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.