更改 javascript 计时器中的字体颜色
Change font color in javascript timer
我有一个在 DIV ID "clear" 内工作的非常简单的计时器,我希望 div 的字体颜色恰好在一分钟标记处改变并再次改变在 2 分钟标记处。好像可以,但是我想不通。
var ho1 = document.getElementsByTagName('ho1')[0],
clear = document.getElementById('clear'),
seconds = 0, minutes = 0, hours = 0,
t;
function add() {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
}
ho1.textContent = (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);
timer();
}
function timer() {
t = setTimeout(add, 1000);
}
timer();
谁能帮帮我?颜色在 DIV 的样式中定义。
HoeInside 你检查 seconds
的 if 条件
if (seconds >= 60) {
seconds = 0;
// check for minutes
minutes++;
if(minutes===1) ho1.style.color = "red";
if(minutes===2) ho1.style.color = "green";
}
使用 jQuery 和 Switch-case:
window.scount = 0;
function changeColor() {
switch (window.scount) {
case 0:
timer('white')
break
case 1:
timer('red')
timer()
break
case 2:
timer('green')
break
default:
console.log('end')
break
}
}
function timer(color) {
$('#clear').css('background-color', color)
setTimeout(changeColor, 1000);
window.scount = window.scount + 1;
}
timer(black);
我有一个在 DIV ID "clear" 内工作的非常简单的计时器,我希望 div 的字体颜色恰好在一分钟标记处改变并再次改变在 2 分钟标记处。好像可以,但是我想不通。
var ho1 = document.getElementsByTagName('ho1')[0],
clear = document.getElementById('clear'),
seconds = 0, minutes = 0, hours = 0,
t;
function add() {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
}
ho1.textContent = (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);
timer();
}
function timer() {
t = setTimeout(add, 1000);
}
timer();
谁能帮帮我?颜色在 DIV 的样式中定义。
HoeInside 你检查 seconds
if (seconds >= 60) {
seconds = 0;
// check for minutes
minutes++;
if(minutes===1) ho1.style.color = "red";
if(minutes===2) ho1.style.color = "green";
}
使用 jQuery 和 Switch-case:
window.scount = 0;
function changeColor() {
switch (window.scount) {
case 0:
timer('white')
break
case 1:
timer('red')
timer()
break
case 2:
timer('green')
break
default:
console.log('end')
break
}
}
function timer(color) {
$('#clear').css('background-color', color)
setTimeout(changeColor, 1000);
window.scount = window.scount + 1;
}
timer(black);