/** * After the document has finished loading */ $(document).ready(function() { loadBlock('/mothersclick.com/front/qa', '#front-qa'); loadBlock('/mothersclick.com/front/group', '#front-group'); }); function loadBlock(path, id) { $.getJSON(path, function(json) { var newIndex; var lastIndex; interval = Math.floor(Math.random() * 2000 + 2000); function callback() { // avoid calling the same index twice in a row do { newIndex = Math.floor(Math.random() * json.length); } while (newIndex == lastIndex); $(id +' li:last').hide('slow', function(){ $(this).remove(); }); $(json[newIndex]).hide().prependTo(id).show('slow'); setTimeout( callback, Math.floor(Math.random() * 2000 + 2000) ); lastIndex = newIndex; } setTimeout( callback, interval ); }); }