2秒内随机更改阵列 [英] random array change in 2 second

查看:68
本文介绍了2秒内随机更改阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,可以让任何人知道如何在2秒内更改此随机关键字.

Hi I have this code can any one know how to change this random keyword in 2 seconds.

function shuffle(a, b) {
    return Math.random() > 0.5 ? -1 : 1;

}

var keywords = ["<div>1</div>", "<div>2</div>", "<div>3</div>", "<div>4</div>", "<div>5</div>", "<div>6</div>", "<div>7</div>", "<div>8</div>", "<div>9</div>", "<div>10</div>", "<div>11</div>", "<div>22</div>", "<div>44</div>", "<div>32</div>", "<div>46</div>"];
var randomKeywords = keywords.sort(shuffle);


function luckcricket() {
    document.write(randomKeywords);
    alert('laad');
}


luckcricket();

推荐答案

您可能是指window.setInterval(function,delay)

function shuffleKeywords() {
    randomKeywords = randomKeywords.sort(shuffle);
}

window.setInterval(function(){shuffleKeywords()},2000);

您也可以使用window.setTimeout(function,delay). setTimeout将在2秒后启动一次.

You could also use window.setTimeout(function,delay). setTimeout will launch after 2 seconds once.

function shuffleKeywords() {
    randomKeywords = randomKeywords.sort(shuffle);
}

window.setTimeout(function(){shuffleKeywords()},2000);

更新:

JS代码:

function shuffle(a,b) {
    Math.random() > 0.5 ? -1 : 1;
}

var keyWord = ["<div>1</div>","<div>2</div>","<div>3</div>","<div>4</div>"];

window.setInterval(function() {
   keyWord = keyWord.sort(shuffle);
   // First line below will append divs to the document, Second line will replace it, use on of them
   document.getElementById('appended').appendChild(document.createTextNode(keyWord));
   document.getElementById('appended').innerHTML = keyWord;
}, 2000);

HTML代码:

<html>
<body>
    <div id="appended"></div>
</body>
</html>

这篇关于2秒内随机更改阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆