无论如何,使用javascript可以让倒计时计时器在特定时间改变颜色吗?
我正在尝试制作一个倒计时计时器,当到达两个不同的点时它会改变颜色,当到达“00:59”时它会变成橙色,然后当到达“00:00”时它会变成红色。如何使用javascript实现这一点无论如何,使用javascript可以让倒计时计时器在特定时间改变颜色吗?,javascript,timer,countdown,Javascript,Timer,Countdown,我正在尝试制作一个倒计时计时器,当到达两个不同的点时它会改变颜色,当到达“00:59”时它会变成橙色,然后当到达“00:00”时它会变成红色。如何使用javascript实现这一点 <html> <head> <title>Countdown</title> <script type="text/javascript"> // set minutes var mins = 1; // calculate the seconds (
<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 1;
// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
var timeout;
function countdown() {
timeout = setTimeout('Decrement()', 1000);
}
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
secs--;
if (secs < 0) {
clearTimeout(timeout);
return;
}
countdown();
}
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return ("0" + mins).substr(-2);
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return ("0" + (secs - Math.round(mins * 60))).substr(-2);
}
</script>
</head>
<body>
<div id="timer">
This is only valid for the next <input id="minutes" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;"> : <input id="seconds" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;">
</div>
<script>
countdown();
</script>
倒计时
//设定分钟数
var-mins=1;
//计算秒数(不要更改此值!除非时间以不同的速度进行…)
var secs=分钟*60;
var超时;
函数倒计时(){
超时=设置超时('Decrement()',1000);
}
函数减量(){
if(document.getElementById){
分钟=document.getElementById(“分钟”);
秒=document.getElementById(“秒”);
//如果剩下不到一分钟
如果(秒<59){
秒。值=秒;
}否则{
minutes.value=getminutes();
seconds.value=getseconds();
}
秒--;
如果(秒<0){
clearTimeout(超时);
返回;
}
倒计时();
}
}
函数getminutes(){
//分是秒除以60,四舍五入
分钟=数学楼层(秒/60);
返回(“0”+分钟).substr(-2);
}
函数getseconds(){
//从总剩余秒数中减去剩余分钟数(以秒计)
返回(“0”+(秒-数学四舍五入(分钟*60)).substr(-2);
}
这仅适用于下一个:
倒计时();
//如果剩下不到一分钟
如果(秒<59){
秒。值=秒;
document.getElementById('timer').style.backgroundColor='#f08000';
}否则{
minutes.value=getminutes();
seconds.value=getseconds();
}
秒--;
如果(秒<0){
document.getElementById('timer').style.backgroundColor='#ff0000';
clearTimeout(超时);
返回;
}
倒计时();
当计时器到达某一点时(如
if
语句中所述),使用DOM更改计时器的颜色。这里就是。我使用颜色数组,存储currentColorIndex,如果它>colors.length,则重置索引。您可以通过为currentColorIndex创建随机数来改进这一点,避免我的无聊循环
var-mins=1;
var secs=分钟*60;
var超时;
函数倒计时(){
超时=设置超时('Decrement()',1000);
}
//魔法从这里开始
var颜色=[“红色”、“绿色”、“蓝色”、“青色”、“洋红”、“黄色”、“黑色”];
var currentColorIndex=0;
函数减量(){
if(document.getElementById){
分钟=document.getElementById(“分钟”);
秒=document.getElementById(“秒”);
//如果剩下不到一分钟
如果(秒<59){
秒。值=秒;
}否则{
minutes.style.color=颜色[currentColorIndex];
seconds.style.color=颜色[currentColorIndex];
minutes.value=getminutes();
seconds.value=getseconds();
如果(++currentColorIndex>colors.length)currentColorIndex=0;
}
秒--;
如果(秒<0){
clearTimeout(超时);
返回;
}
倒计时();
}
}
函数getminutes(){
//分是秒除以60,四舍五入
分钟=数学楼层(秒/60);
返回(“0”+分钟).substr(-2);
}
函数getseconds(){
//从总剩余秒数中减去剩余分钟数(以秒计)
返回(“0”+(秒-数学四舍五入(分钟*60)).substr(-2);
}
倒计时()代码>
这仅对下一个
:
这很简单,在div或分/秒中插入值之前只需添加一个条件
<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 1;
// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
var timeout;
function countdown() {
timeout = setTimeout('Decrement()', 1000);
}
function colorchange(minutes, seconds)
{
if(minutes.value =="00" && seconds.value =="59")
{
minutes.style.color="orange";
seconds.style.color="orange";
}
else if(minutes.value =="00" && seconds.value =="00")
{
minutes.style.color="red";
seconds.style.color="red";
}
}
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
colorchange(minutes,seconds);
secs--;
if (secs < 0) {
clearTimeout(timeout);
return;
}
countdown();
}
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return ("0" + mins).substr(-2);
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return ("0" + (secs - Math.round(mins * 60))).substr(-2);
}
</script>
</head>
<body>
<div id="timer">
This is only valid for the next <input id="minutes" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;"> :
<input id="seconds" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;">
</div>
<script>
countdown();
</script>
倒计时
//设定分钟数
var-mins=1;
//计算秒数(不要更改此值!除非时间以不同的速度进行…)
var secs=分钟*60;
var超时;
函数倒计时(){
超时=设置超时('Decrement()',1000);
}
功能颜色更改(分、秒)
{
如果(分钟值==“00”和秒值==“59”)
{
minutes.style.color=“橙色”;
seconds.style.color=“橙色”;
}
else if(分钟值==“00”和秒值==“00”)
{
分钟数.style.color=“红色”;
seconds.style.color=“红色”;
}
}
函数减量(){
if(document.getElementById){
分钟=document.getElementById(“分钟”);
秒=document.getElementById(“秒”);
//如果剩下不到一分钟
如果(秒<59){
秒。值=秒;
}否则{
minutes.value=getminutes();
seconds.value=getseconds();
}
颜色变化(分、秒);
秒--;
如果(秒<0){
clearTimeout(超时);
返回;
}
倒计时();
}
}
函数getminutes(){
//分是秒除以60,四舍五入
分钟=数学楼层(秒/60);
返回(“0”+分钟).substr(-2);
}
函数getseconds(){
//从总剩余秒数中减去剩余分钟数(以秒计)
返回(“0”+(秒-数学四舍五入(分钟*60)).substr(-2);
}
这仅适用于下一个:
倒计时();
请注意,如果计时器对您来说很重要,那么它就不会精确。我的意思是,如果您要求计时器倒计时60秒,倒计时将需要60秒以上的时间。将计时器间隔设置为1000毫秒时,最短为1秒。
<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 1;
// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
var timeout;
function countdown() {
timeout = setTimeout('Decrement()', 1000);
}
function colorchange(minutes, seconds)
{
if(minutes.value =="00" && seconds.value =="59")
{
minutes.style.color="orange";
seconds.style.color="orange";
}
else if(minutes.value =="00" && seconds.value =="00")
{
minutes.style.color="red";
seconds.style.color="red";
}
}
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
colorchange(minutes,seconds);
secs--;
if (secs < 0) {
clearTimeout(timeout);
return;
}
countdown();
}
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return ("0" + mins).substr(-2);
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return ("0" + (secs - Math.round(mins * 60))).substr(-2);
}
</script>
</head>
<body>
<div id="timer">
This is only valid for the next <input id="minutes" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;"> :
<input id="seconds" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;">
</div>
<script>
countdown();
</script>