【问题标题】:Jquery random words in array without repeating数组中的Jquery随机单词不重复
【发布时间】:2023-03-22 07:04:01
【问题描述】:

我需要在div中显示随机单词而不重复单词。

示例: b 一种 C d

不是: b 一种 b C d d 一种 一种 c

$(document).ready(function($) { 
words = ['a','b','c','d'];
function doSomething() {}
(function loop() {
    var rand = Math.round(Math.random() * (3000 - 500)) + 500;
    setTimeout(function() {
            var thisWord = words[Math.floor(Math.random() * words.length)];
            $("#container").append("<div class=\"conversation\">"+thisWord+"<div class=\"conversation\">");
            doSomething();
            loop();  
    }, rand);
}());
});

【问题讨论】:

标签: jquery random append


【解决方案1】:

您可以使用.splice() 删除words 中显示的字母,如果words 中没有任何内容,则停止显示字母。

jQuery(document).ready(function($) {
  words = ['a','b','c','d'];
  $(function() {
    rand = setInterval(function() {
      var thisWord = words[Math.floor(Math.random() * words.length)];
      $("#container").append(thisWord);
      // Remove the displayed letter from words
      words.splice(words.indexOf(thisWord), 1);
      // If there is nothing left in words, clear the interval.
      if (words.length == 0) {
        clearInterval(rand);
      }
    },800);
  });
});

Fiddle 上的演示

【讨论】:

    【解决方案2】:

    您可以使用$.extend 创建words 数组的副本,然后您可以.splice 删除您添加的每个元素。然后,当数组为空时,您可以清除间隔:

    jQuery(document).ready(function($) {
        var words = ['a','b','c','d'];
    
        $(function() {
            //Create Copy
            var wordsSpliced = $.extend(true, [], words);
    
            //Save Interval Variable
            var intervalAddWords = setInterval(function() {
                //Clear Interval if all words shown
                if (wordsSpliced.length == 0) clearInterval(intervalAddWords);
    
                //Remove random word from wordsSpliced
                var thisWord = wordsSpliced.splice(Math.floor(Math.random() * wordsSpliced.length), 1);
                $("#container").append(thisWord);
            },800);
        });
    });
    

    在这里提琴:http://jsfiddle.net/05bbjtgc/1/

    【讨论】:

      【解决方案3】:

      这是我最新的代码:

      $(document).ready(function($) { 
      words = ['a','b','c','d'];
      function doSomething() {}
      (function loop() {
          var rand = Math.round(Math.random() * (3000 - 500)) + 500;
          setTimeout(function() {
                  var thisWord = words[Math.floor(Math.random() * words.length)];
                  $("#container").append("<div class=\"conversation\">"+thisWord+"<div class=\"conversation\">");
                  doSomething();
                  loop();  
          }, rand);
      }());
      });
      

      我需要在 div 中显示随机单词而不重复单词。

      示例:b a c d

      不是:b a b c d d a a c

      【讨论】:

        【解决方案4】:

        那么,我想知道为什么这段代码不适合您的目的? (还想知道如何稍后发布完全相同的概念并获得更多支持,但这是另一个故事。)

        $(document).ready(function($) {
        words = ['a','b','c','d'];
        var used = new Array();
        var int = null;
        $(function() {
          int = setInterval(function() {
                var r = Math.floor(Math.random() * words.length);
            var thisWord = words[r];
                words.splice(r,1);
            $("#container").append(thisWord);
                if(words.length == 0) {clearInterval(int);$("#container").append(' DONE');}
          },800);
        });
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-10
          • 1970-01-01
          • 1970-01-01
          • 2011-05-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多