title: How to detect if a variable lies in range for more than 2 seconds continuously in JavaScript
body: <p>I'm writing this JavaScript code to detect if a live recording is silent for more than 2 seconds. I used a library to get mic level values and update it to a variable every 100 ms. (the function outputs values between 0-1 if it is silent and value larger than 1 if speech is present). I want to show an alert "You became silent" if the variable <code>levels</code> is in range 0-1 for 2 consecutive seconds.</p>
  
  <pre><code>var levels;
  setInterval(function(){
    levels=meter.volume*1000;
  },100);
  </code></pre>
  
  <p>How do I proceed with this?</p>