Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

114
Vistas
How to make the div box randomly change color every 1000 millisecond with HTML Javascript?

I've been trying to figure out how to make the div box randomly change color every 1000 millisecond. This is my attempt of function changeColor JavaScript file:

myInterval = setInterval(changeColor, 1000);
var box = document.getElementById("box");

function changeColor() {
  var randomColor = Math.floor(Math.random() * 16777215).toString(16);

  box.style.backgroundColor = "#" + randomColor;

  //let x = document.getElementById('contents');
  //x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow";
}
<div id="box">This is some text in a div element.</div>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Like this way

var div = document.querySelector(".test");

setInterval(() => {
  div.style.background = `#${getColor()}`;
}, 1000)

function getColor() {    
  return Math.floor(Math.random()*16777215).toString(16);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

I have attached the javascript function to change the colour of the randomly for every 1000 milliseconds.

<!DOCTYPE html>
<html>
<head>
  <title>Change bg color every 1 seconds</title>
</head>
<body>
<div id="box">This is some text in a div element.</div>
<script>
setInterval(
function () {
  var randomColor = Math.floor(Math.random()*16777215).toString(16);
  document.getElementById("box").style.backgroundColor = "#"+randomColor;
},1000);
</script>
</body>
</html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda