我真的不知道如何为这个JS函数创建时间循环

Don't really know how to create time loop for this JS function

本文关键字:函数 JS 创建 时间 循环 真的不知道      更新时间:2023-09-26

我真的不知道如何为这个JS函数创建时间循环。

函数如下:输入链接描述

只是我需要的是一个时间循环每20秒随机排序..

我使用- var ... = setInterval(..., 20000);

和- var ... = setTimeOut(...,20000);

但是我不知道在哪里连接,或者如果有人知道更好的方法怎么做,一切都会帮助我…

你的尝试有几个问题,下面将做你想做的,只需根据需要调整计时器

更新后的CodePen

   function loop() {  // you had $(function loop(){... here, that is not right
      $container = $('#Container'); 
      if ($container.mixItUp('isLoaded')) { // check if the plugin has been initialized
        $container.mixItUp('sort', 'random', true); // if loaded, just resort
        // change true to false, to forego the animation
      } else { 
        // if not initialized, do that now
        $container.mixItUp({
          load: {
            sort: 'random'
          },
          layout: {
            containerClass: 'list',
            display: 'block'
          }
        });
      }
    }
    $(function() {  // here you had a for loop, not sure why but the int should have been var, anyway, I removed it altogether
         setInterval(loop, 2000);
    });

试试这个:

http://codepen.io/anon/pen/BoZvEx

$(document).ready(function () {
  var mixit = $('#Container').mixItUp({
    load: {
      sort: 'random'
    },
    layout: {
      containerClass: 'list',
      display: 'block'
    }
  });
  function loop() {
    mixit.mixItUp('sort', 'random');
  };    
  var looper = setInterval(loop, 1000);
});

在这里,代码位于$(document)中。准备好了,首先我们实例化一次,在参数中使用配置,然后方法循环只调用一次sort。

$(document).ready(function(){
  setTimeout(function(){
  for( var i = 0; i < 10; ++i )
  {
    loop();
  }
  },20000);
});

我认为你需要这个