如圖是實現的效果
1、實時刷新頁面,顯示數據庫中不斷改變的投票數;
2、如果投票數相同,則名次相同。如圖:
第一個功能用了框架,是在前端寫setInterval方法
第二個功能遇到了一點問題:具體說一下
思路:先根據票數做降序排序,再比較一個節目A的票數與下一個節目B的票數,如果相等,則將B的圖片設置成A;
代碼:
setInterval(function myrefresh()
{
Artery.get("jqGrid18f86").reload();
countList= Artery.get("jqGrid18f86").getValuesArray("jqColumnStringace8e");
nameList = Artery.get("jqGrid18f86").getValuesArray("jqColumnString54dea");
var name = document.getElementsByClassName("actName");
for (var j = 0; j < nameList.length; j++) {
name[j].innerHTML = nameList[j].text;//設置節目名
}
var count = document.getElementsByClassName("actCount");
var num= document.getElementsByClassName("num");
for (var j = 0; j < countList.length; j++) {
count[j].innerHTML = countList[j].text;//設置票數
if(j<countList.length){
if(countList[j].text===countList[j+1].text){
num[j+1].style.backgroundImage=num[j].style.backgroundImage;
}
}
}
},500);
結果運行時報錯Cannot read property 'text' of undefined
經過檢查,沒發現語法問題
於是嘗試換一種寫法,將
if(j<countList.length){
if(countList[j].text===countList[j+1].text){
num[j+1].style.backgroundImage=num[j].style.backgroundImage;
}
改成
if(j>0){
if(countList[j-1].text===countList[j].text){
num[j].style.backgroundImage=num[j-1].style.backgroundImage;
}
沒再報錯。
我認為,可能是瀏覽器在做for循環遍歷數組時,只能獲得當前對象及它之前的對象,當前對象的下一個對象它不能預先獲得(雖然調試時沒問題,因此這是我的猜想)。