elegant js polling routine?

elegant js polling routine?

Hello, I have a requirement where I need to poll the database via ajax from js to check for a status.  If the status is "active" then the polling should stop and an alert should popup "case is now active." The js should check the db every 2 seconds until the db status returns "active."  Can you provide an elegant js routine for this?  Here's some general js to show what I want to do:

function ReportAsActivePoll()
{
   for(var i=0; i<10; i++)
   { 
      setTimeout(StatusIsActive,(i*2000));
      if(statusIsActive)
      {
         ReportAsActive();
         break;
      }
   }
}

var statusIsActive = false;
function StatusIsActive(case)
{
   statusIsActive = GetStatusFromDB(case) == "active";
}

function ReportAsActive()
{
   alert("case is now active")
}

A few notes:

* I know the code above is not correct. It's just for illustrative purposes.
* The code above will call StatusIsActive 10 times. I would like the calls to stop/break/discontinue after status is active. However, I think polling requires to queue up all the calls ahead of time so I'm not sure how to achieve this.