我試圖在4格內隨機切換一些圖片,但我遇到了重複結果的麻煩。 儘管每例如具有避免重複的結果?
1,4,2,3 然後 4,1,3,2
我得到
2,2,4,2,然後1,3,3,4
所以,我需要找到一個解決方案,以避免重複的數字
這是代碼
var tempo = setInterval(function() {
$('.pub').each(function(i) {
var imagens_pubs = ['img_1.jpg', 'img_2.jpg', 'img_3.jpg', 'img_4.jpg'];
var rand = Math.floor(Math.random()*imagens_pubs.length);
$(this).html('<a href="http://www.mysite.com" target="_blank"><img src="pubs/'+imagens_pubs[rand]+'" width="220px" height="50px"></img></a>');
});
}, 5000);
您比較快! – 2012-02-23 11:25:36
感謝您的幫助! – Pluda 2012-02-23 11:41:07