[英]jQuery: function not excecuted twice when using setTimeout
我有以下代碼,函數“ placeNewSponsor”應每4秒重復一次。 (該功能應放置圖像,在3秒鍾后隱藏它們,等待1秒鍾並重復一次)。 但是,當我對此進行測試時,該功能僅執行一次。
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();
您隱藏了容器,但從未重現它們。
$('.sponsorContainer').show(); //show them
setTimeout(function(){ $('.sponsorContainer').hide(); }, 3000); //hides them
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.