Home arrow JavaScript arrow Page 6 - Using Timers in JavaScript

Sliding Around - JavaScript

JavaScript's Window object comes with four methods that you can use to create timed or looping events in your Web pages. In this article, I'll introduce you to them, and also show you a few examples of how they can be used in different situations.

TABLE OF CONTENTS:
  1. Using Timers in JavaScript
  2. Window Washer
  3. New Year Blues
  4. A Decent Interval
  5. Turning Up the Volume
  6. Sliding Around
  7. A Long Wait
By: Nariman K, (c) Melonfire
Rating: starstarstarstarstar / 113
January 28, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVASCRIPT ARTICLES

- Javascript for Beginners: An Introduction
- Introduction to JavaScript
- Adding Elements to a Tree with TreeView jQue...
- Using the Persist Argument in a TreeView jQu...
- Using Unique and Toggle in a TreeView jQuery...
- Using Event Delegation for Mouseover Events ...
- Using the Animate Option in a Treeview jQuer...
- Using HTML Lists with Event Delegation in Ja...
- Opened and Closed Branches on a TreeView jQu...
- Mouseover Events and Event Delegation in Jav...
- Creating a TreeView JQuery Hierarchical Navi...
- Event Delegation in JavaScript
- A Look at the New YUI Carousel Control
- Working with Draggable Elements and Transpar...
- Displaying Pinned Handles with Resizable Con...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: