I'm trying to explain my problem to know the better way to solve it. I've searching a bit, but I don't know how to search exactly:
- I have an HTML page with three areas: Panel A, Grid B and Grid C.
- On grid C, I can do an action on a row (only clicking it) that updates some counters on panel A and Grid B, but they're calculated on database totals.
- When I do the row action I update the row immediately and trigger an event listened by Panel A and Grid B which sends both requests against the server to update it's counters.
Every row update is a bit heavy and if the user clicks various rows fast, the javascript execution is locked flooding the server with updates of Panel A and Grid B which could be deferred to execute only one time if on 1 or 2 seconds the event is not triggered.
I would solve the problem on the listenTo callback because it could be another panel that the event action must be performed "immediately".
I imagine something like this (only refresh after 2 seconds of no event listened), but I think that there must be a better way:
var eventTimeout = {}; // one for listener element.bind('eventName' function() { if (eventTimeout['eventName']) { clearTimeout(eventTimeout['eventName']); // I understand that if the timeout has been exhausted no error is thrown } eventTimeout['eventName'] = setTimeout(function() { eventTimeout['eventName'] = null; doAction(); }, 2000); }); I'll go away with that implementation (I haven't tested yet), when I have more time, I'll put it on a JSFiddle to help to understand.