www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25

Thread: Simplest loop ever, most MAJOR problem.

  1. #1
    Join Date
    Oct 2011
    Posts
    19

    Simplest loop ever, most MAJOR problem.

    Code:
    var i=0;
    	    setTimeout(for(i=0;){
                setTimeout(s1,4000);
    
                setTimeout(s2,8000);
    
                setTimeout(s3,12000);
    
    
    			},8000);

    where functions s1,s2,s3 are already defined.

    I know it is probably going to be the dumbest overlook I ever made but,
    What am I possibly doing wrong?

  2. #2
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    U serious?

    Simplest answer, everything.

    If you need an item to be repeat at a set interval, you use var obj = setInterval(callback,duration) type of arrangement.

    In your example this would likely look like

    var s1obj = setInterval(s1,4000);
    var s2obj = setInterval(s2,8000);
    var s3obj = setInterval(s3,12000);


    the use of obj is in case you wish to cease an interval timer by calling clearInterval( obj ); which will clear that timer.
    You do not nest or "loop" them with a for loop. SetTimeout is not the way to set a timer to repeat because of the time lag that introduced and drag on site performance as your setting many timeouts and an interval only needs setting once.
    Last edited by JunkMale; 10-19-2011 at 04:35 PM. Reason: Stuff to add...

  3. #3
    Join Date
    Oct 2011
    Posts
    19
    So if I want to wait 8 seconds after page loads and then fire a loop with
    3 functions which are to fire at 4 seconds intervals how would I do that?


    (super thanks btw!)

  4. #4
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    well it all depends on how many times you want the our functions to run, from your code and attempt to "loop" I took it to mean you want them to run more than once.

    So you can set up a series of setInterval timers and give them an object that you can use in the s1,s2 & s3 functions to control the timer.

    function startItUp(){

    s1obj = setInterval(s1,4000);
    s2obj = setInterval(s2,8000);
    s3obj = setInterval(s3,12000);

    }

    and all those functions will be called at those number of milliseconds. You would onlu need to wrap them in a function and have the document.body.onload = startItUp(); function to call that function to trigger the functions at regular intervals.
    Last edited by JunkMale; 10-19-2011 at 04:52 PM. Reason: Missin stuff...

  5. #5
    Join Date
    Oct 2011
    Posts
    19
    infinite loop, 4 seconds in between.



    Only exception would be 8 seconds delay before loop is fired.

  6. #6
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Code:
    If you use  a JSON type arrangement then you could...
    
    timer = {
        startItUp:function(){
          .... do stuff like set the other interval timers...
        },
        auto:setTimeout("timer.startItUp()",8000)
    
    }

  7. #7
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    that BTW will set a timeout that calls the startItUp function after 8 seconds. It only runs onces, so any timers you set within that function will run at the intervals you set.

  8. #8
    Join Date
    Oct 2011
    Posts
    19
    I am only using jQuery.

    functions s1,s2,s3 each fire the loading of a different image in a slideshow, that's why the loop is to be infinitely set once it has begun.

  9. #9
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Code:
    timer = {
        startItUp:function(){
          s1obj = setInterval(s1,4000); 
          s2obj = setInterval(s2,8000); 
          s3obj = setInterval(s3,12000);
        },
        auto:setTimeout("timer.startItUp()",8000)
    
    }
    will cause those timers to be set 8 seconds after the page script loads, the page could still be loading which is why I suggested the document.body.onload method to ensure that the function is called when the document loads.

    When set, the functions s1 to 3 will be called at those specified intervals.

    jQuery what? why not learn to do it the easy way through plain old scripting...

  10. #10
    Join Date
    Oct 2011
    Posts
    19
    I don't think it would be fair to ask you to explain what you posted before but after taking your advice blindingly I wanted to let you know...

    IT WORKED

    AND I LOVE YOU!!!! thanks man!!

  11. #11
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    np.

  12. #12
    Join Date
    Oct 2011
    Posts
    19
    but wait!

    there's a bug :'(

    Code:
    s1obj = setInterval(s1,5000);
    s2obj = setInterval(s2,12000);
    s3obj = setInterval(s3,19000);
    s2 fires within 3 seconds, and s3 within 1 second. What is wrong?

  13. #13
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Code:
    s1obj = setInterval(s1,5000);
    s2obj = setInterval(s2,12000);
    s3obj = setInterval(s3,19000);
    strange... try

    Code:
    s1obj = setInterval("s1()",5000);
    s2obj = setInterval("s2()",12000);
    s3obj = setInterval("s3()",19000);
    which might make a difference. I would advice that you load from a fresh the page as you may have interval timers running... once set the interval timers will run until the page is closed or navigated away from.

    I just tried a script with the change to the function call within quotes with () and it worked fine on my browser.

  14. #14
    Join Date
    Oct 2011
    Posts
    19
    I understand the bug.

    each s#obj repeats itself every nnnn miliseconds.

    The way it is built currently:
    if s1obj is fired every 5 seconds and s2obj is fired every 11 seconds then the result would be s1obj firing twice before s2obj fires for the first time.


    Which differs from what I need:

    s1obj fires after 5 seconds, 6 seconds later s2obj is fired, and another 6 seconds later s2obj is fired, Repeat.

  15. #15
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Quote Originally Posted by javaGuyava View Post
    but wait!

    there's a bug :'(

    Code:
    s1obj = setInterval(s1,5000);
    s2obj = setInterval(s2,12000);
    s3obj = setInterval(s3,19000);
    s2 fires within 3 seconds, and s3 within 1 second. What is wrong?
    s1obj = setInterval(s1,5000); has been called twice, 5 seconds + 5 seconds = 10 seconds - 8 seconds pause + 1 second lag = about 3 seconds apparent execution time.

    Just to explain. It appears that to call setInterval(s3, triggers immediately and then at the set interval whereas setInterval("s3()", does not and only is called at the specified interval.

    Its a bit like ++c; or c++; pre or post.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles