Search code examples
javascriptasp-classicblink

blink multiple text using javascript


How to blink multiple text in classic asp respectively? I'm trying to create a timer in my JavaScript. Must work for IE, Firefox and Chrome. Thanks

<script type="text/javascript">
     var col = new String();
     var x = 1;
     var y;

     function blink() {
          if (x % 2) {
               col = "rgb(255,0,0)";
          } else {
               col = "rgb(255,255,255)";
          }

          aF.style.color = col;
          aF1.style.color = col;
          aF2.style.color = col;
          aF3.style.color = col;
          x++;

          if (x > 2)
          { x = 1 }; 
          setTimeout("blink()", 2000);
     }
</script>

Solution

  • You can use the power of closures, I have used your code to set your effect but its better instead of font color change you use jquery fadeIn and fadeOut effect for more smoothy look. And your font effect will only work on text part not on the whole element. so its better either you play with opacity or use jQuery

    function blink(node)
    {
         var x = 1;
         var timer;
         function start_blink()
         {
             if(x%2==0)
             col = "rgb(255,255,255)";
             else
             col = "rgb(255,0,0)";
    
             node.style.color =  col;             
    
             x++;    
             if (x > 2)
              { x = 1 };      
    
         }
         function stop()
         {
           if(timer)
            clearInterval(timer);
          }
         timer = setInterval(start_blink,2000);
    
         return stop;
    }
    

    How to use

    var ele = document.getElementById('whomYouWantToBlink');
    var stopper = blink(ele);
    
    // to stop you can always call stopper() to stop the respective elements blinking effect