【发布时间】:2023-04-04 21:03:01
【问题描述】:
animations = ['fadeIn','fadeInDown','slideInUp','flipInY','bounceInLeft'];
想象一下,每当用户点击某物时,我都会产生随机效果,所以为了获得最佳体验,我希望用户也有相同的效果。但是随着
animations[ Math.floor(Math.random() * animations.length) -1];
那会发生。
如何避免相同的值再次出现?
【问题讨论】:
-
你有 5 种效果。显示相同的值是很自然的。你能提供更多关于
How to avoid same value to appear again?的细节吗? -
您可以为效果索引设置一个变量 i,然后将动画(或它的副本)设置为 animations.splice(i,1)。只要数组变空,你就需要从所有动画重新开始。
-
另外你确定你想要那个-1吗?
-
随机选择一个item,从sources数组中拼接出来,这样就不会再被选中了。
animations.splice(Math.floor(Math.random() * animations.length),1); -
@JeremyKahan splice 将从数组中删除动画名称。
标签: javascript jquery