Home > Enterprise >  Rotating font family with js
Rotating font family with js

Time:10-09

I'm very new to JS, I have a problem that could easily be solved I think but I can't figure it out :

I would like to find a way to change the font-family of an html element every 0.1 seconds without having to trigger something.

Basically I would like the html element to change font every 0.1 sec, rotating amongst 6 font families.

really thankful if you guys can find a way.

Louis

CodePudding user response:

Try something like :

function changefontFamily() {
  var doc = document.getElementById("elementID");
  var font= ["Arial", "Verdana", "Helvetica ", "Tahoma"];
  doc.style.fontFamily= font[i];
  i = (i   1) % font.length;
}
setInterval(changefontFamily, 100);
<div id="elementID">text</div>

CodePudding user response:

I set 1 second (1000 ms), for clarity, because 100ms is too fast

  • Related