2013-08-20 85 views
0

當我應用以下腳本時,它在輸出中隨機出現「undefined」。我檢查過,它應該是由javascript引起的。我可以知道可能是什麼錯誤嗎?帶有「undefined」的javascript隨機圖像

<div id="changeImgNews"></div> 

<script language=Javascript> 
var countNum = <{$block.countNum}>; 

var checkRepNews = 1; 
var ranNumNews = 0; 
var ranContentNews = ""; 
var arti_name = new Array(); 
var arti_img = new Array(); 
var arti_id = new Array(); 
var arti_model = new Array(); 
var arti_desc = new Array(); 

<{foreach from=$block.news item=arti key=count}> 
arti_name[<{$count}>] = "<{$arti.name}>"; 
arti_img[<{$count}>] = "<{$arti.img}>"; 
arti_id[<{$count}>] = "<{$arti.id}>"; 
arti_model[<{$count}>] = "<{$arti.model}>"; 
arti_desc[<{$count}>] = "<{$arti.desc}>"; 
<{/foreach}> 

function randomImageNews() 
{ 
ranNumNews = Math.round(Math.random()*countNum); 
while (ranNumNews == checkRepNews){ 
    ranNumNews = Math.round(Math.random()*countNum+1); 
}; 
checkRepNews = ranNumNews; 
ranContentNews = "<a href='<{$portal_url}>/modules/content/item.php?itemid=16' title='"+arti_name[ranNumNews]+"'><img src='<{$portal_url}>/modules/news/images/news"+arti_img[ranNumNews]+"' style='border: 1px solid #000000; '/width='220' height='220'></a><br /><div style='text-align:left; width:220px;'>"+arti_model[ranNumNews]+" - <a href='<{$portal_url}>/modules/content/item.php?itemid=16'>"+arti_name[ranNumNews]+"</a>"; 
document.getElementById('changeImgNews').innerHTML = ranContentNews; 
setTimeout("randomImageNews();", 8000); 
} 
randomImageNews(); 
</script> 
+1

這將獲得我的代碼審查一個很好的答案是肯定的。非常需要解決。 –

回答

0

使用Math.floor(Math.random()*countNum)而不是Math.round(Math.random()*countNum+1)

P.S.爲了上帝的緣故,重構這個! :-)

+0

謝謝!但它似乎不是數學和數學的問題。我試圖更換,但它仍然沒有幫助。 – user2699714

0

我試圖折射代碼,但沒有測試。試一下。

<script language=Javascript> 
    var randomizer = null, 
     randomizerController = null; 
     article = {}, 
     portalUrl = ''; 

    article = { 
     id : [], 
     name : [], 
     image : [], 
     model : [], 
     desc : [] 
    }; 

    portalUrl = '<{$portal_url}>'; 

    randomizer = function() { 
     var randomIndex = 0, 
      content = ''; 

     randomIndex = Math.floor((Math.random() * article.id.length) + 1); 

     content = "<a href='" + portalUrl + "/modules/content/item.php?itemid=16' title='" + article.name[randomIndex] + "'>"; 
     content += "<img src='" + portalUrl + "/modules/news/images/news" + article.image[randomIndex] +"'"; 
     content += "style='border: 1px solid #000000; '/width='220' height='220'></a><br />"; 
     content += "<div style='text-align:left; width:220px;'>" + article.model[randomIndex]; 
     content += " - <a href='" + portalUrl + ">/modules/content/item.php?itemid=16'>" + article.name[randomIndex] +"</a>"; 

     console.log(content); 
    }; 

    <{foreach from=$block.news item=arti key=count}> 
     article.id.push('<{$arti.id}>'); 
     article.name.push('<{$arti.name}>'); 
     article.image.push('<{$arti.img}>'); 
     article.model.push('<{$arti.model}>'); 
     article.desc.push('<{$arti.desc}>'); 
    <{/foreach}> 

    randomizer = setInterval(randomizerController, 8000); 

</script> 
+0

感謝您的幫助。由於我對javascript的知識有限,恐怕修改整個代碼太複雜了。 – user2699714