Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Infinitely Looping slideshow with delays?

How do I make an infinite loop in JavaScript? I'm trying to make a slideshow, which I have working, but I can't get it to loop. I can't even get it to loop twice.

The code I'm using right now is

window.onload = function start() {     slide(); } function slide() {     var num = 0;     for (num=0;num<=10;num++) {         setTimeout("document.getElementById('container').style.marginLeft='-600px'",3000);         setTimeout("document.getElementById('container').style.marginLeft='-1200px'",6000);         setTimeout("document.getElementById('container').style.marginLeft='-1800px'",9000);         setTimeout("document.getElementById('container').style.marginLeft='0px'",12000);     } } 

Without the for thing in there, it does go through once. When I put in a for, it either makes Firefox lock up, or just loops once. I'm sure this is a really simple thing to do, and even if it has to be loop 1,000,000 times or something instead of infinite, that'd work fine for me.

Also, I don't want to use jQuery or something that someone else created. I'm learning JavaScript, and this is partially to help me learn, and partially because I'm trying to make as many HTML5-based systems as I can.

EDIT: I think the reason it's freezing is because it executes the code all at once, and then just stores it in a cache or something. What I want it to do is go through this once, then start at the top again, which is what I've always thought loops where for. In "batch" (command prompt) scripting, this could be done with a "GOTO" command. I don't know if there's an equivalent in JS or not, but that's really my goal.

like image 321
JacobTheDev Avatar asked Apr 29 '11 17:04

JacobTheDev


People also ask

How do you make an infinite loop in JavaScript?

In for() loop: To avoid ending up in an infinite loop while using a for statement, ensure that the statements in the for() block never change the value of the loop counter variable. If they do, then your loop may either terminate prematurely or it may end up in an infinite loop.

What will happen if while loop runs indefinitely in JavaScript?

An infinite loop is a piece of code that keeps running forever as the terminating condition is never reached. An infinite loop can crash your program or browser and freeze your computer. To avoid such incidents it is important to be aware of infinite loops so that we can avoid them.

What causes an infinite loop in JavaScript?

An infinite loop is a condition where a piece of your JavaScript code runs forever caused by a fault in your code that prevents the loop from getting terminated.

How do you stop an infinite loop in JavaScript?

pause script with F8, Ctrl+\ or by clicking Pause script execution button, press mouse button for 1-3 seconds on the button again to see more options, move click action to square stop button on expanded menu to stop permanently script execution.


1 Answers

The correct approach is to use a single timer. Using setInterval, you can achieve what you want as follows:

window.onload = function start() {     slide(); } function slide() {     var num = 0, style = document.getElementById('container').style;     window.setInterval(function () {         // increase by num 1, reset to 0 at 4         num = (num + 1) % 4;          // -600 * 1 = -600, -600 * 2 = -1200, etc          style.marginLeft = (-600 * num) + "px";      }, 3000); // repeat forever, polling every 3 seconds } 
like image 101
Andy E Avatar answered Oct 12 '22 01:10

Andy E