0

I am trying to get a variable to increment in a function called by setInterval. It does one then stops... All I want is for the slider function to loop through the array over and over again, but it seems to like stopping after the first one. Here is the code:

<script type='text/javascript'>

var i = 0;

function slider(varToSlide)
	{
		document.write(varToSlide[i]);
		i++;
		if(i == 2) { i = 0; }
	}
	
var slides = new Array;

slides[0] = "0";
slides[1] = "1";
slides[2] = "2";

setInterval("slider(slides)", 1000);

</script>
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by rajarajan07
0

Actually I can only get setinterval to run one time. Period. I tried this simple code:

function counter() {
document.write(".");
}
setInterval("counter()", 1000);

and it still only executes the counter function once. I was under the impression that setInterval would keep executing until clearInterval was called?

0

The problem is that document.write cannot be used with setInterval().

Use something like this

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
    <script type="text/javascript">
      function timewaster() {
        document.getElementById("out").appendChild(document.createTextNode("x"));
      }
    </script>
    <title></title>
  </head>
  <body>
    <div id="out"></div>
    <script type="text/javascript">
	var stop = window.setInterval('timewaster()', 1000)
    </script>
  </body>
</html>

instead.

0

One More sample with images

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript">
function timewaster() {
	var divs = document.getElementsByTagName('div');
	for(i=0;i<divs.length;i++){
	  if (divs[i].style.visibility=="visible")
			divs[i].style.visibility="hidden"; 
	  else
			divs[i].style.visibility="visible"; 
	}
}
    </script>
</head>
</body>
 <body>
   <div id='imageSpace' style="visibility: visible;" ><img src='images/image.jpg'></div>
    <script type="text/javascript">
	var stop = window.setInterval('timewaster()', 1000)
    </script>
  </body>
</html>

Place an image and test it

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.