www.webdeveloper.com
Results 1 to 9 of 9

Thread: intervals

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Location
    Australia
    Posts
    23

    intervals

    I am trying to display 4 divs one after eachother using setInterval

    Code:
    function switchIcon(current, next)
    {
    
    	document.getElementById(next).innerHTML = "<div class=\"on\"></div>";
    		document.getElementById(current).innerHTML = "<div class=\"off\"></div>";
    
    	   
    	       
    }
    
    function timedMsg()
    {
    	var IntervalId1 = 0;
    		var IntervalId2 = 1;
    			var IntervalId3 = 2;
    		var IntervalId4 = 3;
    
    
    IntervalId1 =setInterval(switchIcon("stepImage1", "stepImage2"),2000);
        clearInterval ( IntervalId1);
    
    IntervalId2 =setInterval(switchIcon("stepImage2", "stepImage3"),2000);
        clearInterval ( IntervalId2);
    
    IntervalId3 =setInterval(switchIcon("stepImage3", "stepImage4"),2000);
        clearInterval ( IntervalId3);
    
    IntervalId4 =setInterval(switchIcon("stepImage4", "stepImage1"),2000);
        clearInterval ( IntervalId4);
    
    }
    the code is simple,
    onLoad timeMsg() is called
    then after 2000 milliseconds each setInterval should be called. What i am finding is that only the first setInterval call is being fired and then the scripts stops...

    anyone know why...

    could it be that all 4 statements are being fired at the same time?

    thanks in advance

    Ilan

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    Maybe you should be using setTimeout() instead.

  3. #3
    Join Date
    Jan 2005
    Location
    Australia
    Posts
    23
    setTimeout didn't work either...

    i will give it another go though...

    thanks for the reply...

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by iperez_genius View Post
    setTimeout didn't work either...

    i will give it another go though...

    thanks for the reply...
    What you want to do can be done using setTimeout().

    If you post your code using setTimeout() it will be easier to see what is going on.

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    setInterval and setTimeout need a reference of a function as argument. use an anonymous:
    Code:
    IntervalId1 =setInterval(function(){switchIcon('stepImage1','stepImage2')},2000);
    // and so on

  6. #6
    Join Date
    Aug 2007
    Posts
    3,767
    And you will have to use 2000 for the first setTimeout, 4000 for the second etc., because the time is being set from now, not from when the next finishes.

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Declan1991 View Post
    And you will have to use 2000 for the first setTimeout, 4000 for the second etc., because the time is being set from now, not from when the next finishes.
    In fact setTimeout() would be a better choice, I presume.

  8. #8
    Join Date
    Aug 2007
    Posts
    3,767
    Quote Originally Posted by Kor View Post
    In fact setTimeout() would be a better choice, I presume.
    That reminds me, do you only want this to loop once? If you want it to loop multiple times, there's better ways of doing this.

  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    Another thing to consider:
    IntervalId1 =setInterval(switchIcon("stepImage1", "stepImage2"),2000);
    clearInterval ( IntervalId1);
    Why are you clearing the intervals immediately after setting them?

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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