Click to See Complete Forum and Search --> : Explain a code


Sonia
07-25-2003, 01:13 PM
Heeelllooo there, can a nice person explain me what the following code means?

<html>
<head>
<script>

var i =1
var text = "Welcome to my site idiot!!!"

function fn()
{
window.status = text;
var r = text.substr(i,text.length-1)
text = r + text.charAt(0)
setTimeout("fn()",100)
}

</script>
</head>

<body onload = "fn()">
</body>
</html>


I know the code produces a scrolling message in the statusbar but I need a detailed explanation of each line. Can anyone help that pooor girl plzzzzzzzzzz
:D :D :D :D

David Harrison
07-25-2003, 01:52 PM
Declare's a variable i and gives it a value of 1.
Declare's a variable text and gives it a value of "Welcome to my site idiot!!!".

Create's a function, fn().

Inside the function window.status = text; sets the status bar to the same value as text, ie: "Welcome to my site idiot!!!".

var r = text.substr(i,text.length-1)
Create's a local variable r with the value of text from character number i to the length -1.

Characters are numbered as follows:

W e l c o m e
0 1 2 3 4 5 6 etc.

so there are 27 char's in total, but the last one is number 26.

so the variable r just cuts off the first char. of text.

Then text is re-assigned a value of r and something else. That something else is the first char. in the variable text.

So that text now = "elcome to my site idiot!!!W".

If this function were to repeat then the first letter would keep getting taken off the front and tacked onto the back.

To make it repeat there is timeout, what this does is, run a specific function after a certain length of time specified in milliseconds.

In this case it runs the function fn(), which is the one that it is in, so that the process can repeat.

Of course before the function does anything it has to be called so to set the whole thing in motion there is an onload event handler in the body tag that triggers the function in the first place.

rstaticn
07-25-2003, 01:56 PM
var i =1
var text = "Welcome to my site idiot!!!"

these set the variables, I hope this part isn't confusing for you.

function fn()

this names the function

window.status = text;

this sets the status bar at the bottom of the window to equal the value of text

var r = text.substr(i,text.length-1)

this takes the value of text, and chops off the last character and this is the value for r

text = r + text.charAt(0)

this takes the first character of text and puts it at the end of r

setTimeout("fn()",100)

this calls the function again with a 100 millisecond delay inbetween


hope that helps