www.webdeveloper.com
Results 1 to 5 of 5

Thread: A Simple Javascript Loop Which Fires Functions.

  1. #1
    Join Date
    Oct 2011
    Posts
    19

    A Simple Javascript Loop Which Fires Functions.

    I am trying to create a script, which 9 seconds after page load (4s First time it executes only, 5 secs thereafter) executes function s2, waits 7 seconds then fires function s3, waits another 7 seconds then fires function s4, and finally will return back to firing s2 after 5 seconds continuing the previous loop.

    Here is what I have so far:

    Code:
    timer = {
        startItUp:function(){
          s1obj = setTimeout(s2,5000);
          s2obj = setTimeout(s3,12000);
          s3obj = setTimeout(s4,19000);
        },
        auto:setTimeout("timer.startItUp()",4000)
    
    }
    The problem with setTimeout is that after the first round it no longer loops.

    The problem with setInterval is that each functions keeps on looping individually without waiting on other functions to fire first.


    Maybe I am doing it completely wrong but I need a loop that fires functions one after another with set time in between each function fire.

    Anny help will be very appreciated. Thanks ahead of time!!

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Lightbulb Something to consider ...

    Make each setTimeout function call the next one in sequence.

    For example (pseudo-code):
    s1obj = setTimeout(s2,5000);

    function s2() {
    ...
    s2obj = setTimeout(s3,12000);
    }

    function s3() {
    ...
    s3obj = setTimeout(s4,1900);
    }

    function s4() {
    ...
    s1obj = setTimeout(s1,5000);
    }

  3. #3
    Join Date
    Oct 2011
    Posts
    19

    Thumbs up

    Wish I could upvote you or something.

    Thanks man. Lifesaver.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Thumbs up

    Quote Originally Posted by javaGuyava View Post
    Wish I could upvote you or something.

    Thanks man. Lifesaver.
    You're most welcome.
    Happy to help.
    Good Luck!


    BTW: Mark thread as resolved if you are done with the problem.

  5. #5
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    It would help if you explain, as I asked in your other thread about this issue, about what your script is meant to do.


    I eventually thought that something like

    Code:
    slideData = [ {target:".box",flipdata:{direction:'rl',content:"<div class=page><img src=slide1.jpg></div>",color:'#d18a66'},time:5000},
     {target:".box",flipdata:{direction:'rl',content:"<div class=page><img src=slide2.jpg></div>",color:'#d18a66'},time:12000},
     {target:".box",flipdata:{direction:'rl',content:"<div class=page><img src=slide3.jpg></div>",color:'#d18a66'},time:19000} ];
    
    function rotate(){
    	slo = slideData.shift(); // remove the top object
    	slideData.push( slo ); // append it to the end of the array
     return slo;// return the object
    }
     
    function slider(){
    
    	next = rotate(); // grab an object
    
    	$( next.target ).flip( next.flipdata ); // use the data
    	setTimeout("slider()", next.timer ); // get ready for next round.
    }
    
    setTimeout("slider()",4000);
    may do the trick.

    The idea here is you have one function that performs the update rather than replicating the same function format over and over. Using an array that you remove and append an element to means that you are rotating the data in the array and you do not need to track pointers for that array.

    The seTimeout in this instance is appropriate, however you can also use setInterval but each call to the update function would require a call to clearInterval( objref ); where objref is a variable that holds a reference to that timer event.

    eg.
    Code:
    function slider(){
               clearInterval( objref ); // kill the current timer
    
    	next = rotate(); // grab an object
    
    	$( next.target ).flip( next.flipdata ); // use the data
    	objref = setTimeout("slider()", next.timer ); // get ready for next round.
    }
    Does that solve the problem?
    Last edited by JunkMale; 10-20-2011 at 09:37 AM.

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