I am trying to make a text slide show with javascript, however in javascript the math floor function always returns the same random number. What is wrong with code below?
var randomnumber;
D=Array(7)
D[0]='Sunday!'
D[1]='Monday!'
D[2]='Tuesday!'
D[3]='Wednesday!'
D[4]='Thursday!'
D[5]='Friday!'
D[6]='Saturday!'
window.setTimeout("Tick()", 1000);
function Tick()
{
document.write('<marquee><font size="+2">'+D[Math.floor(Math.random()*7)]+'</font></marquee>')
}
</script>
Works fine. However, you must pass a reference to the function when using
setTimeoutand not a string.setTimeoutuses aneval-like procedure when you pass it a string, making this method of usage unsafe.setTimeoutonly fires once when the timeout is reached. If you want to do a constant, continuousTick, then better usesetIntervalinstead, which fires and “reloads” to fire again.Here’s a modified version of the code to fit your description