var myInterval = 0; var i = 0; function startLoop() { if(myInterval > 0) clearInterval(myInterval); // stop myInterval = setInterval( "showBee()", 7000 ); // run } function showBee() { bee = Math.floor(Math.random()*5) + 1; beex = Math.floor(Math.random()*($(document).width()-47)); beey = Math.floor(Math.random()*($(document).height()-43)); i++; var beeRotation = Math.floor(Math.random()*100) - 50; $('#container').append(''); $('.bee' + i).css('top', beey).css('left', beex).css('-moz-transform', 'rotate(' + beeRotation + 'deg)').css('-webkit-transform', 'rotate(' + beeRotation + 'deg)'); } $(document).ready(function() { startLoop(); });