![](/img/trans.png)
[英]Javascript, how do i get two corresponding elements from an array to generate at the same time with the random feature?
[英]How do you use javascript to get a random result from an array over a time interval?
我希望下面的函數tim每500毫秒從Array獲得一條不斷變化的路由。 相反,在我的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
的名稱更改為其他名稱,而不是覆蓋Array
對象。
接下來, _.first
. _.first
似乎不必要。 樣本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( function() { document.body.innerHTML = _.sample(arr); }, 500);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.js"></script>
您需要將一個函數傳遞給setInterval
,而要通過下划線的first
和sample
方法(在您的情況下為字符串)將數組運行的結果傳遞給該函數。 正如@Tony所指出的,因為sample
方法的第二個參數指定了您期望的結果數量(請參閱文檔 ),所以不需要使用first
。
以下是我想您正在尋找的:
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);
您可以使用此函數從數組中獲取隨機元素,如下所示:
var url = getRandomeEle(array);
function getRandomEle(arr) {
return arr[~~(Math.random()*arr.length)];
}
已經有了很好的答案,我也同意Tony的觀點,您確實應該更改變量名稱Array
。
我想您會在某個時候停止此間隔,在這種情況下,您需要調用clearInterval
。
這是一個快速的DEMO 。
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.