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 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    setTimeout() problems.

    hi guys-

    i'm working on a small idea and i would like to run this code:-
    Code:
    document.getElementById(id).style.height = origheight+"px";
    after a set time period (about 20ms i guess ), however, the following line creates errors:-
    Code:
    var t1 = setTimeout("document.getElementById('+id+').style.height = '+origheight+'px' ",0.5);
    any ideas why?

  • #2
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I'm rather new to JS, but I believe its the timing. You have 0.5, which Javascript reads as a half of a milisecond, and I'm not sure if Javascript can read decimals in that field. Anywho, 1000 miliseconds is equal to one second, so 5 seconds = 5000 or 30 seconds = 30,000.

    Hope this helps,

    FuZion

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    you're passing a statement to the setTimeout, which is looking for a function
    Code:
    //assuming you have "id" and "orignheight" already established globally
    var t1=setTimeout(function(){document.getElementById(id).style.height=origheight+"px";},20);

  • #4
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    and.. ms==millisecond - so 20ms isn't half a second - it's a fiftieth of a second. if you want half a second, change the 20 to 500.

  • #5
    New Coder
    Join Date
    Dec 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by subhailc
    you're passing a statement to the setTimeout, which is looking for a function
    Code:
    //assuming you have "id" and "orignheight" already established globally
    var t1=setTimeout(function(){document.getElementById(id).style.height=origheight+"px";},20);
    Cheers fella - am i right in thinking this isn't the best way to run a statement after a set time?

    And yeah, the 0.5 was a typo i meant to write 20.

  • #6
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    no, it's fine. the example i gave you should work.

    ... 20 milliseconds isn't noticeable. you probably want 500, which is half a second and still hardly a delay at all...

  • #7
    New Coder
    Join Date
    Dec 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by subhailc
    no, it's fine. the example i gave you should work.

    ... 20 milliseconds isn't noticeable. you probably want 500, which is half a second and still hardly a delay at all...
    it goes through possibly 1000 iterations so it makes for a nice smooth and quick transformation


  •  

    Posting Permissions

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