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:
Sahil Batra
2022-11-15 20:11:56 +05:30
committed by Tim Abbott
parent 5ce7015cc8
commit f772eeddd6
3 changed files with 24 additions and 31 deletions

View File

@@ -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();
}