SMF For Free Support Forum
Signup For Free Forum
September 06, 2008, 12:22:17 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome to SMF For Free. The best free SMF Host
 
   Home   Help Search Arcade Gallery Login Register  

Pages: [1]
  Print  
Author Topic: Learn how to use the SetInterval() function  (Read 266 times)
0 Members and 1 Guest are viewing this topic.
slayer766
SMF For Free Member
*
Offline Offline

Posts: 36



View Profile WWW
« on: February 04, 2008, 07:27:57 pm »

This tutorial is all about the function setInterval() It works kinda like setTimeout() but instead of the function being executed once and only once, setInterval() executes the function forever until you clear it. So I will show you an example of setInterval():


Code:
<script type="text/javascript">

function Get_It(){
setInterval('alert("I\'m annoying...")',5000);
}
GetIt();
</script>

Yes that will get very annoying if it were to be an actual code that someone used without it being cleared at some point. Which what I wrote will always alert every 5 seconds. Wink



You could use setInterval() to maybe create text effects or make a slide show. I will show you something to the extent of text effects:

Code:
<a href='javascript:Start_It();'>Start the effects</a><br /><a href='javascript:Stop_It();'>Stop the effects</a><br /><br /><span id="effect">This is some text</span><br />

Code:
<script type="text/javascript">

function Start_It(){
it_is = setInterval('Effects();',2000);
}

function Effects(){
document.getElementById("effect").style.color="red";
setTimeout('Effects2();',500);
}

function Effects2(){
document.getElementById("effect").style.color="green";
}

function Stop_It(){
clearInterval(it_is);
}

</script>

Alright what I did was just basically run the Interval every 2000 milliseconds, or 2 seconds, have it start a function to color the text. The put in a setTimeout function to initiate at 500 milliseconds, or 1/2 a second to color it a different color. Then since the Interval function is always going to be running it will color the text red again, and keep on doing that until you click Stop the effects. Which in that function I clear the Interval. Remember that clearing an Interval you must always assign it to a variable, otherwise the function will not know what to clear. Wink
« Last Edit: February 04, 2008, 07:29:34 pm by slayer766 » Logged

Check our Code Index which currently contains 453 codes!



Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC
ServerBeach Coupon
Page created in 0.198 seconds with 17 queries.