Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

Changing the font color of a timer in javascript

I just want to change the font color of my timer to white so it could go well with my preferred background color.

Here's my fiddle. https://jsfiddle.net/fpouew9d/.

var CCOUNT = 30;

var t, count;

function cddisplay() {
  // displays time in span
  document.getElementById('timespan').innerHTML = count;
};

function countdown() {
  // starts countdown
  cddisplay();
  if (count == 0) {
    // time is up
  } else {
    count--;
    t = setTimeout("countdown()", 1000);
  }
};

function cdpause() {
  // pauses countdown
  clearTimeout(t);
};

function cdreset() {
  // resets countdown
  cdpause();
  count = CCOUNT;
  cddisplay();
};
cdreset()
p {
  color: white;
}

body {
  background-color: #242424;
}
<p>
  Some text
</p>

<span id="timespan"></span>
<input type="button" value="Start" onclick="countdown()">
<input type="button" value="Stop" onclick="cdpause()">
<input type="button" value="Reset" onclick="cdreset()">
question from:https://stackoverflow.com/questions/65898420/changing-the-font-color-of-a-timer-in-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, you have setInterval function. You can use it instead of hacks around setTimeout.

To change color of any text in css:

#timespan {
  color: white;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...