If you expect the browser to support CSS animations, you can do something more interesting and perhaps less annoying. Define in the stylesheet:
body { -webkit-animation: changebg 1s infinite cubic-bezier(1,0,0,1); -moz-animation: changebg 1s infinite cubic-bezier(1,0,0,1); animation: changebg 1s infinite cubic-bezier(1,0,0,1); } @-moz-keyframes changebg { 0% {background-color: #f00;} 50% {background-color: #fff;} 100% {background-color: #f00;} } @-webkit-keyframes changebg { 0% {background-color: #f00;} 50% {background-color: #fff;} 100% {background-color: #f00;} } @keyframes changebg { 0% {background-color: #f00;} 50% {background-color: #fff;} 100% {background-color: #f00;} }
And you're done, without JavaScript at all. Unfortunately, CSS animations are not yet standard, so they depend on prefixes, so I had to repeat for -moz-
and -webkit-
. At the moment, it does not work in Opera and IE.
source share