Here is the code:

        
  
Change number depending on how fast you want the colors to change     
// var back = setInterval(function(){ setColor() }, 300);
 
function setColor() {
  var x = document.body;
  x.style.backgroundColor = x.style.backgroundColor == "black" ? "grey" : "black";
}
 
function stopColor() {
  clearInterval(back);
}
// end of loop


        
      

 

Here is an example of the demo in action:

View at full scale