Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

Looping functions

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

godkillah
Senior Member
****


Posts: 261
Group: Registered
Joined: Sep 2007
Status: Offline
Reputation: 3
Points: 183 (Donate)
Post: #1

Looping functions


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

Quote:
<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:

Quote:
<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

Quote:
<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

Quote:
<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:

Quote:
<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


29-09-2007 01:42 AM
Find all posts by this user Quote this message in a reply
ivenms
Administrator
*******


Posts: 2,172
Group: Administrators
Joined: Sep 2006
Status: Offline
Reputation: 14
Points: 4373 (Donate)
Post: #2

RE: Looping functions


Great work.

Working perfectly.. Smile

This is something similar to the tutorial of "Random Quote", I made on here. But this is very effective and simple. Also changes the content without refreshing the page. Very useful for displaying news on your pages.


Read: General Rules & Policies before posting.
Make Money By Posting | Earning and Exchanging Points | Add Your Links
29-09-2007 12:34 PM
Find all posts by this user Quote this message in a reply
godkillah
Senior Member
****


Posts: 261
Group: Registered
Joined: Sep 2007
Status: Offline
Reputation: 3
Points: 183 (Donate)
Post: #3

RE: Looping functions


thanks Smile
i wrote this a some months ago,
i was getting a lot of pm's with requests for help on javascript so i decided to write some tutorials Smile


29-09-2007 07:00 PM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Declaring Variables and Functions on Java Script ivenms 0 564 15-09-2006 03:13 AM
Last Post: ivenms

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Website of the Month

Create-a-Page for Free
SOTM June 2008


Accepting Submissions
for July 2008
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 20-08-2008, 05:24 PM


Copyright © 2002-2008 MyBB Group
Powered By MyBB