Click to See Complete Forum and Search --> : Help plzzzzzzzzzzzzzzzzzzzz
Sonia
07-25-2003, 01:43 PM
Heeelllooo there, can a nice person explain to 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
MikeOS
07-25-2003, 02:21 PM
The first part of the code is simply declaring two global variables:
var i =1
var text = "Welcome to my site idiot!!!"
These will be used in the function that follows. The function is called when the page loads, this is due to the onload event handler in the body tag:
<body onload = "fn()">
The first thing the function does is take the string "Welcome to my site idiot!!!" and assigns it to the windows status bar, which is why you see it there:
window.status = text;
The next parts make it move. First you have the line:
var r = text.substr(i,text.length-1)
This creates a local variable called r, assigned to it, the first time the function runs, is the string "elcome to my site idiot!!!" Note the W is missing, but not lost as it is added on again to the end of the string, and the whole thing is reassigned to the text global variable in this line:
text = r + text.charAt(0)
So the first time this code is executed the text global variable will hold "elcome to my site idiot!!!W" - note the W now at the end. This process repeats with the next letter being added on to the end the next time the function is called etc etc, thus giving the impression of a scroll. The last line:
setTimeout("fn()",100)
ensures that the fn() function keeps getting called over and over, every 100th of a millisecond, which ensures the whole process is repeated.
AdamBrill
07-25-2003, 03:02 PM
Here is a modified version that I made... I thought you might be interested. I put a lot of comments in so you know what is going on. ;)<html>
<head>
<title>Cool scrolling text!</title>
<script type="text/javascript">
var text = "Welcome to my site idiot!!!"; //the string that will scroll
var i =- text.length; //a looping variable
wait_time = 100; //the time that it is set to wait inbetween each letter
extra_space = 0; //the extra space to add
for(x=0; x<extra_space; x++){ //loop through and
text = " "+text; //add the extra spaces
} //end of the for loop
function fn(){ //the function that is run onload
if(i>text.length){ //checks if i is more than the length of text
i=-text.length+extra_space; //it it is, then it sets i to the negative value of the length of text
} //end of the if statement
if(i<0){ //if i is less than 0
line = text.substr(0,i+text.length); //gets from char 0 to char i+the length of text
while(line.length<text.length){ //while the length of line is less than the length of text,
line=" "+line; //add a space before the line to make it look right
} //end the while loop
}else{ //if i is more than or equal to 0
line = text.substr(i); //gets from char i on
} //end the if statement
window.status = line; //sets the status to equal line
i++; //increments i
setTimeout("fn()",wait_time); //sets the function to run again
} //end of the fn() function
</script>
</head>
<body onload = "fn()">
</body>
</html> I hope all of that doesn't confuse you... :D