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 Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact Advertise on Bucaro TecHelp Advertise Here RSS News Feeds News Feeds

HTML5 Solutions: Essential Techniques for HTML5 Developers

Essential Techniques for HTML5 Developers

HTML5 brings the biggest changes to HTML in years. Web designers now have new techniques, from displaying video and audio natively in HTML, to creating realtime graphics on a web page without a plugin.

This book provides a collection of solutions to all of the most common HTML5 problems. Every solution contains sample code that is production-ready and can be applied to any project.

Click Here

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>

RSS Feed RSS Feed



Web Design Sections

Eloquent JavaScript: A Modern Introduction to Programming

Eloquent JavaScript

Eloquent JavaScript Eloquent JavaScript goes beyond the cut-and-paste scripts of the recipe books and teaches you to write code that's elegant and effective. You'll start with the basics of programming, and learn to use variables, control structures, functions, and data structures. Then you'll dive into the real JavaScript artistry: higher-order functions, closures, and object-oriented programming.

Reader Anthony says, "This book is not your typical Javascript book. Others have a utilitarian approach. In stark contrast, Eloquent JavaScript does not merely provide you a checklist of things to learn but rather paints a panorama of the possibilities that programming provides. Javascript is merely the tool used to introduce these to the reader.

Click here for more information.


[Site User Agreement] [Advertise on This site] [Search This Site] [Contact Form]
Copyright©2001-2011 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268