【问题标题】:Randomize a each loop随机化每个循环
【发布时间】:2015-09-03 16:35:10
【问题描述】:

我正在尝试随机化我制作的这个函数,但我只找到了一个随机 TimeOut 解决方案。我希望顺序是随机的,而不是超时。

$(document).ready(function() {
    function boucle() {
        $('#test img').fadeTo(1000, 0);
        $('#test img').delay(1000).each(function(i) {
            $(this).delay((i++) * 1500).fadeTo(1500, 1);
        });
    }
    boucle();
    setInterval(boucle, 15000);
});

我试过this solution 但这不是我想要的。而this one 对我不起作用。

有人可以帮我做那个随机循环吗?提前谢谢你。

编辑:

这是我如何实现@jack 提供的解决方案:

(function($) {
          $.fn.shuffle = function() {
            // credits: http://bost.ocks.org/mike/shuffle/
            var m = this.length, t, i;

            while (m) {
              i = Math.floor(Math.random() * m--);

              t = this[m];
              this[m] = this[i];
              this[i] = t;
            }

            return this;
          };
        }(jQuery));

        function boucle() {
            $('#test img').fadeTo(1000, 0);
            $('#test img').shuffle().delay(1000).each(function(i) {
                $(this).delay((i++) * 1500).fadeTo(1500, 1);
            });
        }
        boucle();
        setInterval(boucle, 10000);

【问题讨论】:

  • 您的意思是要随机化each() 遍历元素的顺序?
  • @RoryMcCrossan 完全正确!

标签: jquery random


【解决方案1】:

这是一个示例,说明如何在使用 .each() 对其进行迭代之前对 jQuery 的结果数组进行洗牌(使用 Fisher-Yates-Knuth 洗牌)。

顺便说一句,您也可以使用相同的技术来打乱页面上的元素。

(function($) {
  $.fn.shuffle = function() {
    // credits: http://bost.ocks.org/mike/shuffle/
    var m = this.length, t, i;

    while (m) {
      i = Math.floor(Math.random() * m--);

      t = this[m];
      this[m] = this[i];
      this[i] = t;
    }

    return this;
  };
}(jQuery));

jQuery(function($) {
  $('div').shuffle().each(function() {
    console.log($(this).text());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>

【讨论】:

  • 成功了!谢谢,出于学习目的,您使用的语法是在 jquery 库中创建函数吗?这样做而不是像我的“Boucle”函数那样传统地创建函数有什么好处?
  • @ThierryDalleau 它创建了一个 jQuery 插件,可以在需要时重复使用。
  • 好的。所以这段代码必须在我的 jquery.js 文件中?
  • @ThierryDalleau 不,你可以把它放在任何你喜欢的地方
【解决方案2】:

如果你可以使用query-js,你可以像这样随机排序:

$(document).ready(function() {
    function boucle() {
        var i = 0;
        new Query($('#test img').fadeTo(1000, 0).delay(1000))
        .orderBy(function() {
            return Math.random();
        }).iterate(function(t) {
            $(t).delay((i++) * 1500).fadeTo(1500, 1);
        });
    }
    boucle();
    setInterval(boucle, 15000);
});

这是利用 jquery 选择充当数组的事实

【讨论】:

  • 使用Math.random() 的结果对数组进行排序不是我推荐的简单随机操作操作。在最坏的情况下,你会得到 O(n lg n) 运行时间而不是 O(n)。
  • 更糟糕的是,it's not randomly distributed
  • @Bergi 不是 orderBy 的情况。每次需要比较时,排序都会选择一个随机数。另一方面,orderBy 将一个随机数关联到每个元素并重用该相同的数字。即 orderBy 像这样使用时将对随机数列表进行排序。因此,分布与使用的随机数生成器一样好
  • @Jack 你怎么认为 [0.0,1.[ * m 比 [0.0,1.0] 分布更广
  • @RuneFS 我不是在争论,我是说直接使用Math.random()的输出作为排序比较函数的返回值是错误的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
相关资源
最近更新 更多