this might sound noob-ish question, that's because i'm pretty new @ js. couldn't find information on anywhere, figured i'd ask community.
how make infinite loop in javascript? i'm trying make slideshow, have working, can't loop. can't loop twice.
the code i'm using right
window.onload = function start() { slide(); } function slide() { var num = 0; (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 thing in there, go through once. when put in for, either makes firefox lock up, or loops once. i'm sure simple thing do, , if has loop 1,000,000 times or instead of infinite, that'd work fine me.
also, don't want use jquery or else created. i'm learning javascript, , partially me learn, , partially because i'm trying make many html5-based systems can.
edit: think reason it's freezing because executes code @ once, , stores in cache or something. want go through once, start @ top again, i've thought loops for. in "batch" (command prompt) scripting, done "goto" command. don't know if there's equivalent in js or not, that's goal.
the correct approach use single timer. using setinterval, can achieve want follows:
window.onload = function start() { slide(); } function slide() { var num = 0, style = document.getelementbyid('container').style; window.setinterval(function () { // increase num 1, reset 0 @ 4 num = (num + 1) % 4; // -600 * 1 = -600, -600 * 2 = -1200, etc style.marginleft = (-600 * num) + "px"; }, 3000); // repeat forever, polling every 3 seconds }
Comments
Post a Comment