wouldn't it be great to make a function repeat everytime without getting your browser crashed?
here i'm going to explain you how to do this!
we're going to write a script that changes some html to a random message, so lets first write an array so it would look like
<script>
var rawr=new Array()
rawr[0]="I'd like to have an cookie"
rawr[1]="Ya ever wanted to eat your computer?"
rawr[2]="Sometimes theres only 1 right word, 'Rawr'"
</script>
well looks fine so far, now before we edit the html we will write the html. we will add this above the script so we get:
<span id="Pattrick">If you see this than you messed up!</span>
<script>
var rawr=new Array()
rawr[0]="I'd like to have an cookie"
rawr[1]="Ya ever wanted to eat your computer?"
rawr[2]="Sometimes theres only 1 right word, 'Rawr'"
</script>
you'll i added a <span> with the id 'pattrick' also because we will edit the innerHTML of it you will only see whats between it if the script doesnt work, clear to you so far?
now we're going to create our function that changes the innerHTML of the span
<span id="Pattrick">If you see this than you messed up!</span>
<script>
var rawr=new Array()
rawr[0]="I'd like to have an cookie"
rawr[1]="Ya ever wanted to eat your computer?"
rawr[2]="Sometimes theres only 1 right word, 'Rawr'"
function RAWRZ(){
document.getElementById("Pattrick").innerHTML="the new innerHTML"
}
RAWRZ()
</script>
would work fine so far but wouldn't do what we want it to do, we're going to change the innerHTML to rawr[number] wich number will be a random number, but how do we get a random number? math will help us! lets write a variable that gets a random number for this so we'll get
<span id="Pattrick">If you see this than you messed up!</span>
<script>
var rawr=new Array()
rawr[0] = "I'd like to have an cookie"
rawr[1] = "Ya ever wanted to eat your computer?"
rawr[2] = "Sometimes theres only 1 right word, 'Rawr'"
function RAWRZ(){
a = Math.floor(Math.random()*rawr.length);
document.getElementById("Pattrick").innerHTML=rawr[a]
}
RAWRZ()
</script>
scripts would work so far but we want it to repeat itself so we will be using the setTimeout() to let it repeat every 5 secconds,
now we'll be having this:
<span id="Pattrick">If you see this than you messed up!</span>
<script>
var rawr=new Array()
rawr[0] = "I'd like to have an cookie"
rawr[1] = "Ya ever wanted to eat your computer?"
rawr[2] = "Sometimes theres only 1 right word, 'Rawr'"
function RAWRZ(){
a = Math.floor(Math.random()*rawr.length);
document.getElementById("Pattrick").innerHTML=rawr[a]
a = setTimeout("RAWRZ()",5000)
}
RAWRZ()
</script>
Conclusion, just use the setTimeout() in the function to activate itself, the timeout should always be greater than 0 else your browser will crash