Javascript: requestIdleCallback with timeout is always fired immediately

108 views Asked by At

I want to send analytics data to server at low priority. My function is:

const schedule_serversync = function (deadline) {
    if (deadline.timeRemaining() > 0) {
        server_sync(); //async ajax call of approx few ms duration
    }
    requestIdleCallback(schedule_serversync, { timeout: 60000 });
}

As soon as I call requestIdleCallback(schedule_serversync, { timeout: 60000 }); from onload, schedule_serversync is called immediately and the requests go into infinite tight loop. This happens even if the window is not idle (I'm clicking, moving mouse etc, but the callbacks continue).

I know I can break the tight loop using setTimeout while rescheduling requestIdleCallback.

My question is - shouldn't schedule_serversync be called only when browser is idle with max delay of 60 seconds? What defines idle here? I was expecting it to be fired when user moves to some other browser tab or window. Is there a way I can set minimum delay (other than using setTimeout of my own)?

0

There are 0 answers