javascript - auto-shuffle divs every n seconds -


i have here code random divs same class name. need keep randomizing them every n seconds (15 seconds instance) without need refresh page. http://jsfiddle.net/yxbhh/

js code:

var parent = $("#shuffle"); var divs = parent.children(); while (divs.length) {     parent.append(divs.splice(math.floor(math.random() * divs.length), 1)[0]); } 

does have ideas how it? i'm still not used javascript yet appreciated.

you can use setinterval function:

var $parent = $("#shuffle");  var $divs = $parent.children();    setinterval(function() {    var $clone = $divs.slice();    while ($clone.length) {      $parent.append($clone.splice(math.floor(math.random() * $clone.length), 1));    }  }, 2000);
.hue {    background: #ddd;  }    .hue:nth-child(2n) {    background: yellow;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="shuffle">    <div class="hue">one</div>    <div class="hue">two</div>    <div class="hue">three</div>    <div class="hue">four</div>  </div>


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -