Daily Archives: 2011-12-14

Blink Element’s Border Javascript Function

I need a piece of javascript code to make the border of an element blink infinite.
So I write the next function, that changes element’s border color (using DOM) to 2nd passed color code. Then make infinite recursion calls of this function, after the passing time by using setTimeout("javascript statement",milliseconds); Use anonymous function as statement to setTimeout for passing altered color parameters each time, set 1st color code to 2nd and vice versa. After every recursive call set all params to null preventing memory leaks.

* @author Petros Kyladitis - 2011
* @license MIT License
* @description make the border of an element blink
* @param colorA first border's color code
* @param colorB second border's color code
* @param elementId element id
* @param time blinking time in milliseconds

function blinkBorder(colorA, colorB, elementId, time){
  document.getElementById(elementId).style.borderColor = colorB ;
  setTimeout( function(){
    blinkBorder(colorB, colorA, elementId, time);
    colorB = null;
    colorA = null;
    elementId = null;
    time = null;
  } , time) ;