ページのさまざまなセクションに (3 つのパレットから) ランダムな背景色を適用しています。ただし、同じ色が 2 回続けて表示されないようにしたいです。
do
while
ちょっとしたループがうまくいくと思っていたのですが、見た目ではうまくいきません。
var colours = new Array('#FF5A5A', '#FFBE0D', '#00DDB8');
var divs = $('.row');
var last;
var next;
// for each section
divs.each(function(i){
// get a random colour
do {
next = Math.floor(Math.random()*3);
// if it's the same as the last one, try again!
} while( next === last ) {
next = Math.floor(Math.random()*3);
}
// when it's different to the last one, set it
$(this).css('background-color', colours[next] );
// tell it this is the last one now
next = last;
});
何か案は?