Daanlomm Daanlomm - 3 months ago 16
Javascript Question

jQuery: function not excecuted twice when using setTimeout

I have the following code and the function "placeNewSponsor" should repeat itself every 4 seconds. (the function should places images, hides them after 3 secondes, waits 1 seconds and repeat itself). But when I test this, the the function only executes once.



function placeNewSponsor() {
$('.sponsorContainer').each(function() {

var imageCount = $(".imageContainer").children().length;

do {
randomInt = Math.floor(Math.random() * imageCount + 1);
} while ($.inArray(randomInt, usedNumbers) !== -1);

usedNumbers.push(randomInt);
var randomImage = $('.imageContainer a:nth-child(' + randomInt + ')').clone();

$(this).append(randomImage);
});

usedNumbers = [];

// Hide after 3 seconds
setTimeout(function() {
$('.sponsorContainer').hide();
}, 3000);

// Re-activate function
setTimeout(placeNewSponsor, 4000);
}

placeNewSponsor();




Answer

You hide the container, but you never reshow them.

$('.sponsorContainer').show();  //show them
setTimeout(function(){ $('.sponsorContainer').hide(); }, 3000);  //hides them