如何使用javascript在一段时间间隔内从数组中获取随机结果?

如何使用javascript在一段时间间隔内从数组中获取随机结果?,javascript,underscore.js,lodash,Javascript,Underscore.js,Lodash,我希望下面的函数tim每500毫秒从阵列获取一条不断变化的路由。相反,在我的JS控制台中,我收到以下重复错误: VM5550:1未捕获的语法错误:输入意外结束 这里发生了什么?我需要做什么才能得到我想要的结果 我的代码: var Array = ["http://files.parsetfss.com/9.png", "http://files.parsetfss.com/5.png", "http://files.parsetfss.com/6.png", "http://files.par

我希望下面的函数tim每500毫秒从阵列获取一条不断变化的路由。相反,在我的JS控制台中,我收到以下重复错误:

VM5550:1未捕获的语法错误:输入意外结束

这里发生了什么?我需要做什么才能得到我想要的结果

我的代码:

var Array = ["http://files.parsetfss.com/9.png", 
"http://files.parsetfss.com/5.png",
"http://files.parsetfss.com/6.png",
"http://files.parsetfss.com/7.png",
"http://files.parsetfss.com/8.png"];

var sampl = _.first(_.sample(Array, 1));
var tim = setInterval(sampl, 500);
我还尝试用如下函数包装示例:

var sampl = function(){return _.first(_.sample(Array, 1))};

我仍然没有得到期望的结果,但现在我得到的结果是一致的,甚至是7或11(在多次重新刷新屏幕后)。

您可以使用此函数从数组中获取随机元素,如下所示:

var url=getRandomeEle(数组);


首先,您应该明确地将
var
的名称更改为其他名称,而不是覆盖
数组
对象

接下来,第一个似乎没有必要。1的示例返回一个字符串

运行代码段以查看所需的效果

var-arr=[
"http://files.parsetfss.com/9.png", 
"http://files.parsetfss.com/5.png",
"http://files.parsetfss.com/6.png",
"http://files.parsetfss.com/7.png",
"http://files.parsetfss.com/8.png"
];
var tim=setInterval(函数(){
document.body.innerHTML=\ u0.sample(arr);
},500);

您需要将一个函数传递给
setInterval
,而您要传递的是通过下划线的
first
sample
方法(在您的例子中是字符串)运行数组的结果。正如@Tony指出的,使用
first
是不必要的,因为
sample
方法的第二个参数指定了您期望的结果数量(请参阅)

以下内容符合我认为您正在寻找的内容:

var array = ["http://files.parsetfss.com/9.png", 
"http://files.parsetfss.com/5.png",
"http://files.parsetfss.com/6.png",
"http://files.parsetfss.com/7.png",
"http://files.parsetfss.com/8.png"];

function getRandom() {
    return _.sample(array, 1)[0];
} 

function showRandom() {
    console.log(getRandom());
}

// setInterval returns a pointer to the interval ...
var interval = setInterval(showRandom, 500);

// ... which you can use to stop it
//clearInterval(interval);

答案已经很好了,我同意Tony的观点,即您确实应该更改变量名
Array

我假定您希望在某个时间点停止此间隔,在这种情况下,您需要调用
clearInterval

这里有一个简单的例子

script.js

var intervalId,

    startButton = document.getElementById('start'),
    stopButton  = document.getElementById('stop'),

    urls = [
      "http://files.parsetfss.com/9.png", 
      "http://files.parsetfss.com/5.png",
      "http://files.parsetfss.com/6.png",
      "http://files.parsetfss.com/7.png",
      "http://files.parsetfss.com/8.png"
    ];

startButton.addEventListener('click', tim, false);
stopButton.addEventListener('click', stopTim, false);


function tim() {
  intervalId = setInterval(printRandomUrl, 500);
}

function stopTim(){
  clearInterval(intervalId)
}

function printRandomUrl(){
  console.log(_.sample(urls));
}
index.html

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" href="style.css" />
    <script data-require="jquery" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script data-require="lodash.js@3.8.0" data-semver="3.8.0" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.8.0/lodash.js"></script>

  </head>

  <body>
    <h1>Basic plunk!</h1>
     <button id="start">Start</button>
     <button id="stop">Stop</button>

     <script src="script.js"></script>
  </body>

</html>

基本砰砰!
开始
停止

arr[Math.round(Math.random()*arr.length)]
类似这样的东西您不太清楚您想做什么。
tim
不是函数,
sampl
也不是函数,即使您似乎这样对待它们。您必须显示完整的代码。我们无法重现您的问题,有很多事情没有定义,例如
\uucode>。此外还有
的问题etInterval
需要一个函数作为第一个参数。@Juhana,这就是为什么我尝试将它包装到函数中,但也没有效果。没有比这更多的代码了,我只想每隔500毫秒从数组返回一个不同的路由。也许我走错了方向。我愿意尝试其他方法,但什么?@SpencerWieczorek,uu是loda吗我不确定你想要结果去哪里。你的返回似乎与它没有任何关系,这可能是你的问题。这看起来也很有希望。我想将结果连接到一个随机变化的变量,放在前端的旋转木马上。我能用不带document.body.innerHTML的结果来做这件事吗?如果你想得到结果保存到变量后,只需将
document.body.innerHTML
替换为变量名。上面的语法是为了让代码片段更有价值。何必费心重新设置
.sample
?虽然op使用下划线,但他不标记它
sample
从数组返回一个随机元素。甚至让我们选择一个样本大小,。此方法不添加任何内容。op也没有提到随机值是否应该来自尚未显示的值。这看起来很有希望。我这样做是因为我想创建多个变量,其值每500毫秒更改一次,以便我可以将每个值连接到前端的旋转木马。很难看出这是否有效,因为我无法获得结果JS控制台中的t;只有一个数字。@rashadb-好的,尝试将更新的版本粘贴到下划线站点上的控制台中。我可以理解为什么您现在使用第一个,尽管我可能会像上面一样,通过索引获取第一个元素。
<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" href="style.css" />
    <script data-require="jquery" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script data-require="lodash.js@3.8.0" data-semver="3.8.0" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.8.0/lodash.js"></script>

  </head>

  <body>
    <h1>Basic plunk!</h1>
     <button id="start">Start</button>
     <button id="stop">Stop</button>

     <script src="script.js"></script>
  </body>

</html>