JavaScript
  Home arrow JavaScript arrow Page 6 - Using Timers in JavaScript
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Using Timers in JavaScript
By: Nariman K, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 81
    2004-01-28

    Table of Contents:
  • Using Timers in JavaScript
  • Window Washer
  • New Year Blues
  • A Decent Interval
  • Turning Up the Volume
  • Sliding Around
  • A Long Wait

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Using Timers in JavaScript - Sliding Around


    (Page 6 of 7 )

    Let's look at a couple more examples, which demonstrate just how useful - and varied - these functions can be. This first one uses the setInterval() method to create a timed slideshow:


    <html>
    <head>
    <script language="JavaScript">
    // slideshow image counter
    var i = 0;
    function slideShow() 
    {
     
    // set up list of images
     slidesArray = new Array()
     
     slidesArray[0]="image1.jpg"
     slidesArray[1]="image2.jpg"
     slidesArray[2]="image3.jpg"
     slidesArray[3]="image4.jpg"
     slidesArray[4]="image5.jpg"
     
     // load image
     document.images[0].src=slidesArray[i]
     
     // increment counter
     i=i+1;
     
     // when at the end, start again
     if(i==5) { i=0; }
    }
    </script>
    </head>
    <body onLoad="javascript:setInterval('slideShow()',5000);">
    <img src="image1.jpg" name="image">
    </body>
    </
    html>

    Here, the images to be displayed are stored in a JavaScript array, and a "for" loop takes care of pulling out the correct image from the array and loading it into the page. The setInterval() method is used to refresh the image with a new one every 5 seconds.

    You can also use the setInterval() method to move things around on a Web page, by incrementing an X- or Y-coordinate of a page element at a predefined interval. Consider the following example, which demonstrates by moving a <div> containing an image of a car across the screen.


    <html>
    <head>
    <script language="JavaScript">
    function 
    moveCar() 
    {
     if(
    document.all
     
    {
      document
    .all("car").style.pixelLeft+=1;
      
    if(document.all("car").style.pixelLeft >= (document.body.offsetWidth-30))
       document
    .all("car").style.pixelLeft=-30;
     
    }
     if(
    document.layers
     
    {
      parseInt
    (document.car.left+=5); 
      
    if(window.innerWidth-parseInt(document.car.left)<0)
      document
    .car.left=-30;
     
    }
     
    }
    </script>
    </head>
    <body onLoad="javascript:setInterval('moveCar()', 1)">
    <!-- layer containing car image -->
    <div id="car" style="position:absolute; left:20; top:200; width: 150;
    height:50;">
    <img src="rolls_royce.jpg" width=400 height=500 alt="" border="0">
    </div>
    </body>
    </html>

    More JavaScript Articles
    More By Nariman K, (c) Melonfire


     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway