title: Eye blink timer
body: <p>I'm creating this website with few mascots and I'll implement a "eye blink timer" where I'll make every mascot blink.</p>
  
  <p>My question here is: how do i implement (and how long it is?) the delay between blinks and the blink itself, on any language (i'll probably use Javascript, but it doesn't matter right now).</p>
  
  <p>Is there any resource about a "blink algorithm" or something like that?</p>
  
  <p>Edit: I know how to use setTimeout and setInterval, my question here is more about the algorithm than the implementation itself.</p>
  
  <hr>
  
  <p>Final result:</p>
  
  <pre><code>var blink = {
      delay: function() {
          return Math.random() * 8000 + 2000;
      },
      duration: function() {
          return 100 + Math.floor(Math.random() * 100);
      },
      blinkAgain: function() {
          return (Math.random() &lt; .2);
      },
      betweenBliks: function() {
          return blink.duration() / 2;
      }
  };
  
  $.fn.blink = function(continueBlinking) {
      var $element = $(this);
  
      // Star the blink
      $element.addClass('blink');
  
      // Finish the blink
      setTimeout(function() {
          $element.removeClass('blink');
  
          // Change of blinking again
          if (blink.blinkAgain()) {
              setTimeout(function() {
                  $element.blink(false);
              }, blink.betweenBliks());
          }
      }, blink.duration());
  
      // Continue blinking?
      if (continueBlinking) {
          setTimeout(function() {
              $element.blink(true);
          }, blink.delay());
      }
  };
  </code></pre>