其他分享
首页 > 其他分享> > 多张图图片随机展示,且在第一轮随机中每个图片都能展示出来

多张图图片随机展示,且在第一轮随机中每个图片都能展示出来

作者:互联网

使用闭包实现YYDS
export function RandomMascot() { const imgList = ['1', '2', '3']; let newList = imgList.slice(0); const imgUrl = function () { let data; if (newList.length === 1) { data = newList[0]; newList = []; } else { data = newList[Math.floor(Math.random() * newList.length)]; newList.splice( newList.findIndex((item) => item === data), 1 ); } if (!newList.length) { newList = imgList.slice(0); } console.log(newList, '==newList==', data); if (data) { return data; } else { imgUrl(); } }; return imgUrl; } export const randomMascot = RandomMascot();

 

标签:const,展示,newList,imgList,length,随机,data,imgUrl,图片
来源: https://www.cnblogs.com/lvlvlv/p/16023546.html