<h1 id="block ">This is my header block</h1>
To dynamically change the color of the "block" word every 2 seconds using JavaScript:
let colors = ["blue", "green", "pink", "purple"];
let curColor = 0;
const changeColor = () => {
let text = document.querySelector("#block");
text.innerHTML = text.innerText.replace("block", `<span style="color: ${colors[curColor]}">block</span>`);
curColor++;
if (curColor > 3) {
curColor = 0;
}
setTimeout(changeColor, 2000);
}
setTimeout(changeColor, 2000);
This code will change the color of the word "block" within the h1 tag every 2 seconds.
If needed for another element, replace "#block" in the querySelector with the appropriate selector.