setTimeout() & setInterval() for DOM slideshow

Discussion in 'Javascript' started by Noh Way, Feb 23, 2004.

  1. Noh Way

    Noh Way Guest

    I have a stack of DOM elements and I have stored their IDs in an array. I
    want to press a button - this code is from that button's event's function-
    and have a slideshow as each element is brought to the top, in turn. All
    this, with a user definable delay (currently fixed in the code at 5 secs). I
    cannot get the loop to stop while the interval timer is counting the time.

    Help please :eek:?

    Greg

    ===code below====

    var delayed = 5000;
    var numberOfSteps = picDescPairArray.length; //this array contains element
    IDs for items in a z-order stack
    //if there is more than one item in the array, make the z-index 3 for all
    of them
    if (numberOfSteps > 1){
    //change the z-index of all the items to 3
    for (i=0; i <= (picDescPairArray.length-1); i++){
    document.getElementById(picDescPairArray).style.zIndex = 3;
    }

    //change the z-index of each item to 5 in turn,
    //(changing the previous item's z-index back to 3, providing it isn't the
    first one)
    for (j=0; j <= (picDescPairArray.length-1); j++){
    if (j >= 1){document.getElementById(picDescPairArray[j-1]).style.zIndex =
    3;}//the previous item goes down the stack

    //a test line without the timer code - seems to work?
    //document.getElementById(picDescPairArray[j]).style.zIndex = 5;
    alert('next?');

    //the next item comes up to the top after a delay of 5 secs - well, it
    should???
    a=setInterval("document.getElementById(picDescPairArray[j]).style.zIndex
    = 5;",delayed);
    // I tried - setTimeout('expr',delayed) ...without the clearInterval(a),
    obviously ;o)
    }
    }
    clearInterval(a);
     
    Noh Way, Feb 23, 2004
    #1
    1. Advertising

  2. On Mon, 23 Feb 2004 01:21:35 -0000, Noh Way <> wrote:

    > I have a stack of DOM elements and I have stored their IDs in an
    > array. I want to press a button - this code is from that button's
    > event's function - and have a slideshow as each element is brought
    > to the top, in turn. All this, with a user definable delay
    > (currently fixed in the code at 5 secs). I cannot get the loop to
    > stop while the interval timer is counting the time.


    Your code requires some restructuring, which I've attempted below.
    Unfortunately, without the containing page, I can't test it so I'll have
    to leave it to you.

    If there are any further problems, please post a URL, or a sample of the
    HTML, for this. I, and others, can then check any solutions before posting
    them.

    var delayed = 5000;
    var numberOfSteps = picDescPairArray.length;
    var timerID = null;

    if ( numberOfSteps > 1 ) {
    var j = 0;

    setZIndex( getObjRef( picDescPairArray[ 0 ]), 5 );
    for ( var i = 1; i < numberOfSteps; ++i ) {
    setZIndex( getObjRef( picDescPairArray[ i ]), 3 );
    }

    timerID = setInterval( function() {
    setZIndex( getObjRef( picDescPairArray[ j++ ]), 3 );
    setZIndex( getObjRef( picDescPairArray[ j ]), 5 );
    if( j == numberOfSteps - 1 ) clearInterval( timerID );
    }, delayed );
    }

    function getObjRef( id ) {
    if ( document.getElementById ) {
    return document.getElementById( id );
    } else if ( document.all ) {
    return document.all[ id ];
    }
    return null;
    }

    function setZIndex( obj, index ) {
    if( obj && obj.style && 'undefined' != typeof obj.style.zIndex )
    obj.style.zIndex = index;
    }

    Hope that helps,
    Mike

    --
    Michael Winter
    d (replace ".invalid" with ".uk" to reply)
     
    Michael Winter, Feb 23, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. barry
    Replies:
    4
    Views:
    3,854
    barry
    Apr 3, 2005
  2. Felix Piskunov

    Problem with setInterval(callserver,[period])

    Felix Piskunov, Nov 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    468
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Nov 6, 2006
  3. Robert Mark Bram

    SetTimeout and SetInterval - since when?

    Robert Mark Bram, Jul 10, 2003, in forum: Javascript
    Replies:
    1
    Views:
    141
    Lasse Reichstein Nielsen
    Jul 10, 2003
  4. Weston C

    Trouble using setTimeout/setInterval

    Weston C, Feb 22, 2004, in forum: Javascript
    Replies:
    1
    Views:
    208
    Lasse Reichstein Nielsen
    Feb 22, 2004
  5. Aamir Ghanchi

    setTimeout setInterval specs

    Aamir Ghanchi, Aug 19, 2004, in forum: Javascript
    Replies:
    5
    Views:
    123
    Thomas 'PointedEars' Lahn
    Aug 21, 2004
Loading...

Share This Page