title: Run content script loop only when tab is active
body: <p>I am working on a chrome extension that uses content scripts to runs a loop. The tasks ran in the loop can consume quite some memory, and when having a lot of open tabs it impacts on the browser performance.</p>
  
  <p>I am looking to run the loop only when the tab is active.</p>
  
  <p>I am using <code>onMessage.addListener</code> my background page, then I can <code>sendMessage</code> from the content script and check the <code>sender.tab.id</code> against <code>chrome.tabs.getSelected</code>. But since sendMessage is asynchronous, I am forced to use <code>setInterval</code> for the "is tab active" check to update the variable that allows the loop to run or not.</p>
  
  <p>Is there a better way to find if a tab is active from content scripts?</p>