title: Chrome Extension setTimeout and Messaging
body: <p>I am trying to create (or rather learn) Chrome extension development. I am trying to achieve a simple task between popup and background pages. So far, I have the pages in place and popup can send message to background and background page replies. I am currently lost in creating the behavior I want: </p>
  
  <ul>
  <li>Have popup send a number to background page</li>
  <li>Have background page run during the duration of that number</li>
  <li>When the countdown is done, return the message the popup at the time of completion.</li>
  </ul>
  
  <p>At this point, the background process sends the response immediately and I am sure I am not doing it right. Here is some code</p>
  
  <p>Popup:</p>
  
  <pre><code>chrome.extension.sendMessage({
  method: "popup",
  message: "remindMe",
  now: parseInt(localStorage.timeNow),
  timeToRemindIn: 5
   },
  
    function(response) {
      console.log("sendMessage");
  console.log(response.farewell);
  //alert(response.farewell);
  console.log("sendMessage after");
  </code></pre>
  
  <p>Background: </p>
  
  <pre><code>chrome.extension.onMessage.addListener(
        function(request, sender, sendResponse) {
          //console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension");
  
          if (request.message == "remindMe") {
              startTime();
              if(localStorage.timeToRemindIn &gt; now) {
                  sendResponse({farewell: "reminder to check"});
              }
          } else {
              sendResponse({farewell: "hai hai"});
          }
       });
  
      function startTime() {
          now = parseInt(now) + 1;
          if(localStorage.timeToRemindIn &gt; now) {
              console.log("Reminder to check this tab Yo! " + now);
              return;
          }
          if (localStorage.timeToRemindIn &lt;= now) {
              timerId = window.setTimeout(startTime, pollInterval);
          }
      }
  </code></pre>
  
  <p>});</p>