Js randomly selects color from 10 colors to pick a different color each time

  • 2020-03-26 21:40:28
  • OfStack

Yesterday, when js randomly selected colors from 10 colors, and each time the color was different, we considered a lot, and finally used the following to achieve:
 
var colorList = ["#FFFF99","#B5FF91","#94DBFF","#FFBAFF","#FFBD9D","#C7A3ED","#CC9898","#8AC007","#CCC007","#FFAD5C"]; 
for(var i=0;i<lineList.length;i++){ 
var bgColor = getColorByRandom(colorList); 
} 
function getColorByRandom(colorList){ 
var colorIndex = Math.floor(Math.random()*colorList.length); 
var color = colorList[colorIndex]; 
colorList.splice(colorIndex,1); 
return color; 
} 

This allows you to pick random colors that are different each time

Related articles: