Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
I created time using toLocaleString() and then I use this code to change the color every sec but it work only last color not for all

I create time using toLocaleString() and then I use this code to change the color every sec but it work only last color not for all.

var time=document.getElementById("time");

setInterval(function() {
  var da=new Date();
  var ti=da.toLocaleTimeString();
  // console.log("yes");
  time.innerHTML=ti;
}, 1000);

setInterval(function() {
  var arr=["red","blue","green"];
  var i=Math.floor(Math.random() * 2) + 1;
  // console.log(i);
  time.style.color=time.classList.add(arr[i]);
  console.log(i);
}, 1000);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can try this trick to achieve the requirement you have.

To get the colors in sequence, try this :

var time=document.getElementById("time");
var arr=["red","blue","green"];
let index = 0;

setInterval(function() {
  var da = new Date();
  var ti = da.toLocaleTimeString();
  time.innerHTML = ti;
  time.style.color = arr[index];
  if (index < arr.length) { index++; } else { index = 0 }
}, 1000);
<div id="time"></div>

To get the random color from an array, try this :

var time=document.getElementById("time");
var arr=["red","blue","green"];

setInterval(function() {
  var da = new Date();
  var ti = da.toLocaleTimeString();
  time.innerHTML = ti;
  const index = Math.floor(Math.random() * arr.length);
  time.style.color = arr[index];
}, 1000);
<div id="time"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!