Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts

    setInterval problem - what's wrong here?

    Hi all,

    I'm trying to use setInterval to make a movement effect for a div. Here's the code:
    Code:
    onload=function() {
      nowIDheight=document.getElementById("t").style.height;
      nowIDheight=nowIDheight.substr(0, nowIDheight.length-2); //to remove px
      for (i=0; i<=100; i++)
        setInterval(function(){document.getElementById("t").style.height=i+"px"}, 1000);
    }
    ...
    <div style="height: 100px;border: 1px solid red" id="t">bla</div>
    But that doesn't do the trick.
    What's wrong here?
    Last edited by b_hole; 10-31-2006 at 10:58 PM. Reason: irrelevant question, new one

  • #2
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    New question, sorry for the mess.

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    what you need is to put the logic the for loop provides you inside the interval like so:

    Code:
    var nowID = document.getElementById("t");
    var nowIDheight=nowID.style.height;
    var nowIDheight=nowIDheight.substr(0, nowIDheight.length-2); //to remove px
    var maxNowIDheight = nowIDheight+100;
    function moveNowID()
    {
      nowID.style.height = (++nowIDheight)+"px";
      if (nowIDheight == maxNowIDheight)
        clearInterval(moveInterval);
    }
    var moveInterval  = setInterval(moveNowID, 1000);


  •  

    Posting Permissions

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