you can do this using setInterval method:-
var interval=500,i=0;
setInterval(function(){
  i++;
   if (i % 2 !== 0) {
     document.getElementById('square1').style.visibility = 'visible';
     document.getElementById('square2').style.visibility = 'hidden';
   } else {
     document.getElementById('square1').style.visibility = 'hidden';
     document.getElementById('square2').style.visibility = 'visible';
   }
},interval);
if you want it to go active after it reached a point on the page use an if construct to verify the scrollTop of the page:-
if(document.body.scrollTop>=500){
  ...
  //do some stuff
}
1
solved How to make an html element appear and disappear again and again with Js?