2017-04-03 68 views
-2

我一直在试图为此找到答案,但我无法真正地将其解决。 我需要JavaScript代码来显示一个随机数25次,每个数字有320ms的延迟。 (忽略除//开始滚动其他的东西)以320ms的延迟显示一个随机数25次Js

function roll() { 

var win = Math.floor(Math.random() * 25) + 0 
//change the button when rolling 
rollButton.disabled = true; 
rollButton.innerHTML = "Rolling..."; 
rollButton.style.backgroundColor = "grey"; 
rollButton.style.color = "black" 
setTimeout(unDisable, 8000) 
//start roll 
(insert code here) 


} 

谢谢如果你能帮助

回答

0

你可以做一个简单的事情很简单:

function roll() { 
 
    //change the button when rolling 
 
    rollButton.disabled = true; 
 
    rollButton.innerHTML = "Rolling..."; 
 
    rollButton.style.backgroundColor = "grey"; 
 
    rollButton.style.color = "black" 
 
    setTimeout(unDisable, 8000) 
 
    //start roll 
 
    showNum(25) 
 
} 
 
const getRand = _ => Math.floor(Math.random() * 25) + 0 
 
const showNum = n => { 
 
    if (n-- <= 0) {return} 
 
    document.getElementById("dispNum").innerHTML = getRand() 
 
    setTimeout(_ => showNum(n), 320) 
 
}

它将继续产生一个新的线程来打印一个随机数,同时减少其迭代次数,直到它达到0

+0

您可以编写代码,以便它打印一个随机数0-24到:的document.getElementById( “dispNum”)的innerHTML – shtabbbe

+0

完成!我已更改代码以反映 –

+0

非常感谢! – shtabbbe

1

您可以使用setInterval进行一些延迟的循环,并使用clearInterval来停止该循环!

$(function(){ 
 

 
t = 0; 
 
var interval = setInterval(function(){ 
 
    var win = Math.floor(Math.random() * 25) ; 
 
    var html = $('span').html(); 
 
    $('span').html(html + win + '<br>') 
 
    t++; 
 
    if(t == 25) 
 
    stop(); 
 
}, 320); 
 
function stop(){ 
 
    clearInterval(interval); 
 
} 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<span></span>