次のコードがここで定義された色の配列を循環しない理由を誰か教えてください:
var colors = ["white", "yellow", "orange", "red"];
問題のコード行は次のとおりです。
setInterval(function(){
currElm.style.background = colors[(nextColor++)%(colors.length)]);
}, 500);
それはうまくいくように思われ、このようなコードがカラー サイクリング エフェクトを生成する例をいくつか見てきました。上記 (または以下) のコードに問題がある人はいますか?
関数全体 (進行中の作業):
function setHighlight(elmId, index, songLength){
//alert("called set highlight, params are " + elmId + " " + index + " " + songLength);
var colors = ["white", "yellow", "orange", "red"];
var nextColor = 0;
if(index < 10)
index = "000" + (index);
else if (index < 100)
index = "000" + (index);
else if(index < 1000)
index = "0" + (index);
if(index >= 1000)
index = index;
//alert("called set highlight, params are " + elmId + " " + index + " " + songLength);
//this will be useful for finding the element but pulsate will not work, need to research animations in javascript
var mainElm = document.getElementById('active_playlist');
var elmIndex = "";
for(var currElm = mainElm.firstChild; currElm !== null; currElm = currElm.nextSibling){
if(currElm.nodeType === 1){
var elementId = currElm.getAttribute("id");
if(elementId.match(/\b\d{4}/)){
elmIndex = elementId.substr(0,4);
alert(currElm.getAttribute('id'));
if(elmIndex == index){
setInterval(function(){
currElm.style.background = colors[(nextColor++)%(colors.length)]);
}, 500);
}
}
}
}//end for
}
すべてのヘルプは大歓迎です。ありがとう