I have a problem. I wish to flash (or blink) text from yellow to grey, but I would like the yellow text to remain displayed longer than the grey text.
The code that I have works for an equal duration for each color.
function flashtext(ele,col) {
var tmpColCheck = document.getElementById( ele ).style.color;
if (tmpColCheck === 'grey') {
document.getElementById( ele ).style.color = col;
} else {
document.getElementById( ele ).style.color = 'grey';
}
}
setInterval(function() {
flashtext('flashingtext','yellow');
}, 700 ); //set an interval timer up to repeat the function
Any ideas?
You can use
setTimeout:DEMO: http://jsfiddle.net/EfpEP/
Edit:
But it can be improved a bit:
DEMO: http://jsfiddle.net/EfpEP/3/
Alnitak had the great idea of storing the color because the browser can change it to another syntax. But his code is calling
document.getElementByIdeach time. Then, taking his idea, I think the best way is:DEMO: http://jsfiddle.net/EfpEP/4/
Edit 2:
But if you are going to use something like
you will end up freezing the browser.
Instead, you should use
You can also call passing an element by reference:
flash.add(document.getElementById('flashingtext'),'yellow')(maybe you have a variable which contains an element which has no id).But try not to add more elements after
flash.start(). If you do that, the element will be black (or default color) untilmainis called (maybe 1.4 seconds).DEMO: http://jsfiddle.net/EfpEP/6/