mirror of
https://github.com/zulip/zulip.git
synced 2025-11-11 01:16:19 +00:00
util: Add call_function_periodically helper function.
This commit adds call_function_periodically helper function which will be used to call functions periodically using setTimeout. Currently, this new function is used to send presence requests and trying reload.
This commit is contained in:
@@ -360,3 +360,24 @@ export function get_time_from_date_muted(date_muted) {
|
||||
}
|
||||
return date_muted * 1000;
|
||||
}
|
||||
|
||||
export function call_function_periodically(callback, delay) {
|
||||
// We previously used setInterval for this purpose, but
|
||||
// empirically observed that after unsuspend, Chrome can end
|
||||
// up trying to "catch up" by doing dozens of these requests
|
||||
// at once, wasting resources as well as hitting rate limits
|
||||
// on the server. We have not been able to reproduce this
|
||||
// reliably enough to be certain whether the setInterval
|
||||
// requests are those that would have happened while the
|
||||
// laptop was suspended or during a window after unsuspend
|
||||
// before the user focuses the browser tab.
|
||||
|
||||
// But using setTimeout this instead ensures that we're only
|
||||
// scheduling a next call if the browser will actually be
|
||||
// calling "callback".
|
||||
setTimeout(() => {
|
||||
call_function_periodically(callback, delay);
|
||||
}, delay);
|
||||
|
||||
callback();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user