var rows = ['first', 'second', 'third']; var totalChilds = 0; var selectedGallery = ''; var selectedindex = ''; var selectedElement=''; var randname=''; var balanceval=''; function startRandomizer(){ window.setTimeout( function(){ var number = Math.floor(Math.random() * 3) selectedGallery = rows[number]; totalChilds = $('#' + selectedGallery + ' .item').length; randname = names[ Math.floor(Math.random() *names.length-1)]; selectedindex = Math.floor(Math.random() * totalChilds)-1; if (number != 2) { selectedElement = $('.item').eq(selectedindex).children().last().attr('src'); alertify.message('

'+randname +' got

'+'
').delay(4); } else{ } startRandomizer(); }, Math.random() * 8000 + 2000); // From 10 to 110 secconds } startRandomizer();