title: Running a debounced function after user has navigated away from page
body: <p>I have a debounced function of 3 seconds that I send off to an API service for tracking events.</p>
  
  <pre><code>// api.js
  import { debounce } from 'lodash'
  
  const submitRecords = debounce(async () =&gt; {
    await API.submit({ ...data })
  
    // do other stuff here
  }, 3000)
  </code></pre>
  
  <p>Every time there's a user interaction, my application calls <code>submitRecords</code>, waits 3 seconds, and then makes a request to the API service. The problem here is that if the user navigates away before 3 seconds, the call never gets made. </p>
  
  <p>Is there a way to still send the debounced request even when the user has navigated away from the current URL? I read up on <code>window.onbeforeunload</code> but I'm not sure if it's suitable for my use case.</p>