开发者

Javascript cycling through numbers to appear in document

开发者 https://www.devze.com 2023-03-14 01:48 出处:网络
I\'m trying to cycle through 10 numbers (1 - 9) on the screen every second. Sort of like the green matrix code from the movie..

I'm trying to cycle through 10 numbers (1 - 9) on the screen every second. Sort of like the green matrix code from the movie..

here is my code, I cant for the life of me figure out what I'm doing wrong, I've tried many other things but this seems the most correct to me:

<html>
<head>

<script type="text/javascript">

function numberScroll(){
开发者_JAVA技巧
n = setInterval("Math.floor(Math.random()*11",100);
setInterval("document.getElementById('txt').innerHTML=n",100);

}

</script>
</head>

<body onLoad="numberScroll()">
<div id="txt"></div>
</body>
</html>


You should never pass a string to setInterval/setTimeout.

Use a function instead:

setInterval(function() {
    var n = Math.floor(Math.random() * 11);
    document.getElementById('txt').innerHTML = n;
}, 100);

http://jsfiddle.net/ThiefMaster/Tmqbk/


setInterval(function(){document.getElementById('txt').innerHTML=Math.floor(Math.random()*11)},100);
0

精彩评论

暂无评论...
验证码 换一张
取 消