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>

Recommended Answers

All 3 Replies

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?

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.

Member Avatar for rajarajan2017

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.