demos/network-idle.js
function networkIdleCallback(fn, options = {timeout: 0}) {
  // Call the function immediately if required features are absent
  if (
    !('MessageChannel' in window) ||
    !('serviceWorker' in navigator) ||
    !navigator.serviceWorker.controller
  ) {
    DOMContentLoad.then(() => fn({didTimeout: false}));
    return;
  }

  const messageChannel = new MessageChannel();
  navigator.serviceWorker.controller
      .postMessage(
          'NETWORK_IDLE_ENQUIRY',
          [messageChannel.port2],
      );

  const timeoutId = setTimeout(() => {
    const cbToPop = networkIdleCallback.__callbacks__
        .find(cb => cb.id === timeoutId);

    networkIdleCallback.__popCallback__(cbToPop, true);
  }, options.timeout);

  networkIdleCallback.__callbacks__.push({
    id: timeoutId,
    fn,
    timeout: options.timeout,
  });

  messageChannel.port1.addEventListener('message', handleMessage);
  messageChannel.port1.start();
}
function networkIdleCallback(fn, options = {timeout: 0}) {
  // Call the function immediately if required features are absent
  if (
    !('MessageChannel' in window) ||
    !('serviceWorker' in navigator) ||
    !navigator.serviceWorker.controller
  ) {
    DOMContentLoad.then(() => fn({didTimeout: false}));
    return;
  }

  const messageChannel = new MessageChannel();
  navigator.serviceWorker.controller
      .postMessage(
          'NETWORK_IDLE_ENQUIRY',
          [messageChannel.port2],
      );

  const timeoutId = setTimeout(() => {
    const cbToPop = networkIdleCallback.__callbacks__
        .find(cb => cb.id === timeoutId);

    networkIdleCallback.__popCallback__(cbToPop, true);
  }, options.timeout);

  networkIdleCallback.__callbacks__.push({
    id: timeoutId,
    fn,
    timeout: options.timeout,
  });

  messageChannel.port1.addEventListener('message', handleMessage);
  messageChannel.port1.start();
}
function networkIdleCallback ( fn , options = { timeout : 0 } ) { // Call the function immediately if required features are absent if ( ! ( 'MessageChannel' in window ) || ! ( 'serviceWorker' in navigator ) || ! navigator . serviceWorker . controller ) { DOMContentLoad . then ( ( ) => fn ( { didTimeout : false } ) ) ; return ; } const messageChannel = new MessageChannel ( ) ; navigator . serviceWorker . controller . postMessage ( 'NETWORK_IDLE_ENQUIRY' , [ messageChannel . port2 ] , ) ; const timeoutId = setTimeout ( ( ) => { const cbToPop = networkIdleCallback . __callbacks__ . find ( cb => cb . id === timeoutId ) ; networkIdleCallback . __popCallback__ ( cbToPop , true ) ; } , options . timeout ) ; networkIdleCallback . __callbacks__ . push ( { id : timeoutId , fn , timeout : options . timeout , } ) ; messageChannel . port1 . addEventListener ( 'message' , handleMessage ) ; messageChannel . port1 . start ( ) ; }
networkIdleCallback works similar to requestIdleCallback,
detecting and notifying you when network activity goes idle
in your current tab.
@param {*} fn - A valid function
@param {*} options - An options object
networkIdleCallback works similar to requestIdleCallback detecting and notifying you when network activity goes idle in your current tab .