Continuously add and remove class to a random element with jQuery -


let's have unordered list of ten elements.
i'd class added 1 of them @ random, , remove class after couple of seconds , start again randomly chosen element indefinitely.

what cleanest way achieve that?

edit: i've got far:

<ul id="hideandseek">   <li>...</li>   <li>...</li>   <li>...</li>   <li>...</li>   ... </ul> 

and jquery:

var random = math.floor(math.random() * 1000);  var shownelement = $("#hideandseek li"); shownelement.eq(random % shownelement.length).addclass("shown"); 

however, not run continuously, , don't know how set delay before removing class.

you need use setinterval create timer, , can choose random number , set class item index.

something this:

html

<ul>     <li>one</li>     <li>two</li>     <li>three</li>     <li>four</li>     <li>five</li>     <li>six</li>     <li>seven</li>     <li>eight</li>     <li>nine</li>     <li>ten</li> </ul> 

javascript (w/ jquery)

setrandomclass(); setinterval(function () {     setrandomclass(); }, 2000);//number of milliseconds (2000 = 2 seconds)  function setrandomclass() {     var ul = $("ul");     var items = ul.find("li");     var number = items.length;     var random = math.floor((math.random() * number));     items.removeclass("special");     items.eq(random).addclass("special"); } 

here working example


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -