Sometimes you need something to happen after a delay, or you need something to occur repeatedly. This can be accomplished using Java Script timers. My objective in this article is to give you the timer code without surrounding it with a lot of confusing peripheral code so you can quickly use it for your own purposes.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Easy Java Script Timer Code

Sometimes you need something to happen after a delay, or you need something to occur repeatedly. This can be accomplished using Java Script timers. To create a single delay you would use the setTimeout method. You could then cancel the timeout with the clearTimeout method. To create a repeating event, you would use the setInterval method. You could then cancel the timer with the clearInterval method.

Single Call

timerID = setTimeout(functionName(),milliseconds)
clearTimeout(timerID)

Repeating Calls

timerID = setInterval(functionName(),milliseconds)
clearInterval(timerID)

My objective in this article is to give you the timer code without surrounding it with a lot of confusing peripheral code so you can quickly use it for your own purposes. For that reason, Ill give you examples of functions that use a timer first. Examples of how to call those functions second. And I'll show you the example function called by the timers last. Lets do a repeating event call first.

<script type="text/javascript">
var timerID;

function startTimer()
{
  timerID = window.setInterval("reverseText()", 2000);
}
</script>

Shown above is a Java Script code block containing a variable to hold the timmer's ID and a function named startTimer that executes the setInterval method, passing it the name of a function to call, reverseText(), after the time interval of 2000 milliseconds (2 seconds). The startTimer function returns the timer's ID which is saved in the variable timerID.

Whatever the reverseText() function does will be repeated every 2000 milliseconds until the clearInterval method is called. Below, I've added a function named stopTimer which calls the clearInterval method, passing it the timerID returned by the setInterval method.

<script type="text/javascript">
var timerID;

function startTimer()
{
  timerID = window.setInterval("reverseText()", 2000);
}

function stopTimer()
{
  window.clearInterval(timerID);
}
</script>

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Slide Show Code
Easy Slide Show Code with Mouseover Pause
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Java Script Timer Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Easy Expanding Menu Code
Web Designer's Reference
Object-Oriented JavaScript
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Code for Java Script Cube⁄Box Volume Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Disable IE8 Accelerators on Your Website
Prevent Automated Form Submissions With Java Script
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using the JavaScript Cookie Method
Password Protection Using the Frames Method
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Java Script Comparison Operators
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
Java Script Trim Function
Convert Mixed Number to Decimal
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV


[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2009 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269