多张图图片随机展示,且在第一轮随机中每个图片都能展示出来
使用闭包实现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();