var intervalId;
var attachAnimationByColor = function(id,from,to,milliSeconds){
window.setTimeout(function(){
if(intervalId){
window.clearInterval(intervalId);
}
intervalId = window.setInterval(function(){
var el=document.getElementById(id);
if(!el&&intervalId){
window.clearInterval(intervalId);
return;
}
var curColor = el.style.color;
if(curColor==to){
curColor=from;
}else{
curColor=to;
}
el.style.color = curColor;
}, milliSeconds);
}, 500);
var attachAnimationByColor = function(id,from,to,milliSeconds){
window.setTimeout(function(){
if(intervalId){
window.clearInterval(intervalId);
}
intervalId = window.setInterval(function(){
var el=document.getElementById(id);
if(!el&&intervalId){
window.clearInterval(intervalId);
return;
}
var curColor = el.style.color;
if(curColor==to){
curColor=from;
}else{
curColor=to;
}
el.style.color = curColor;
}, milliSeconds);
}, 500);
}
attachAnimationByColor('typeFont','green','crimson',500);
<span id="typeFont" style="color: crimson; font-weight: bold; font-size: 14px; height: 20px;">大白菜</span>