tags:

views:

1598

answers:

10

I'm displaying a set of images as an overlay using googlemaps. Displaying these images should be in an endless-loop. Most most browsers detect this, and display a warning. Is there a way to make a endless-loop in javascript that is'nt stopped by the browser?

A: 

An endless loop? You are displaying an infinite number of images?

RichardAtHome
+8  A: 

Try setInterval or setTimeout

EDIT: Like in this simple example: http://n0p.com/so/63011.php

Erik
A: 

Perhaps try using a timer which retrieves the next image each time it ticks, unfortunately i don't know any JavaScript so I can't provide a code sample

Crippledsmurf
+3  A: 

You should use a timer to continuously bring new images instead of an infinite loop. Check the setTimeout() function. The caveat is that you should call it in a function that calls itself, for it to wait again. Example taken from w3schools

<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout("timedCount()",1000);
}
</script>
</head>

<body>
<form>
<input type="button" value="Start count!"
onClick="timedCount()">
<input type="text" id="txt">
</form>
</body>

</html>
Vinko Vrsalovic
This is more convoluted than simply using setInterval...
Jason Bunting
+1  A: 

Instead of using an infinite loop, make a timer that keeps firing every n seconds - you'll get the 'run forever' aspect without the browser hang.

Paul Betts
A: 

function foo() { alert('hi'); setTimeout(foo, 5000); }

Then just use an action like "onload" to kick off 'foo'

Wyatt
+2  A: 

The following code will set an interval and set the image to the next image from an array of image sources every second.

function setImage(){
  var Static = arguments.callee;
  Static.currentImage = (Static.currentImage || 0);
  var elm = document.getElementById("imageContainer");
  elm.src = imageArray[Static.currentImage++];
}
imageInterval = setInterval(setImage, 1000);
Marius
A: 

If it fits your case, you can keep loading new images to respond to user interaction, like this website does (just scroll down).

A: 

Just a formal answer:

var i = 0;

while (i < 1) {
   do something...

   if (i < 1) i = 0;
   else i = fooling_function(i);  // must return 0
}

I think no browser would detect such things.

Thevs
A: 

Thevs, browsers will prompt you if any operation is taking too long. Your loop is not an exclusion. The only way is to use setInterval/setTimeout (as it has been already noted).

Vitaly Sharovatov