mirror of https://github.com/vector-im/riot-web
Merge pull request #1731 from matrix-org/dbkr/rate_limited_func_time_from_finish
Make ratelimitedfunc time from the function's endpull/21833/head
commit
383ef8b2ef
|
@ -35,13 +35,17 @@ module.exports = function(f, minIntervalMs) {
|
||||||
|
|
||||||
if (self.lastCall < now - minIntervalMs) {
|
if (self.lastCall < now - minIntervalMs) {
|
||||||
f.apply(this);
|
f.apply(this);
|
||||||
self.lastCall = now;
|
// get the time again now the function has finished, so if it
|
||||||
|
// took longer than the delay time to execute, it doesn't
|
||||||
|
// immediately become eligible to run again.
|
||||||
|
self.lastCall = Date.now();
|
||||||
} else if (self.scheduledCall === undefined) {
|
} else if (self.scheduledCall === undefined) {
|
||||||
self.scheduledCall = setTimeout(
|
self.scheduledCall = setTimeout(
|
||||||
() => {
|
() => {
|
||||||
self.scheduledCall = undefined;
|
self.scheduledCall = undefined;
|
||||||
f.apply(this);
|
f.apply(this);
|
||||||
self.lastCall = now;
|
// get time again as per above
|
||||||
|
self.lastCall = Date.now();
|
||||||
},
|
},
|
||||||
(self.lastCall + minIntervalMs) - now,
|
(self.lastCall + minIntervalMs) - now,
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue