...

View Full Version : setTimeout() problems.



limpsharp
07-01-2006, 09:45 PM
hi guys-

i'm working on a small idea and i would like to run this code:-

document.getElementById(id).style.height = origheight+"px";
after a set time period (about 20ms i guess ;) ), however, the following line creates errors:-

var t1 = setTimeout("document.getElementById('+id+').style.height = '+origheight+'px' ",0.5);
any ideas why?

FuZion
07-02-2006, 04:17 AM
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

subhailc
07-02-2006, 07:23 AM
you're passing a statement to the setTimeout, which is looking for a function

//assuming you have "id" and "orignheight" already established globally
var t1=setTimeout(function(){document.getElementById(id).style.height=origheight+"px";},20);

subhailc
07-02-2006, 07:24 AM
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.

limpsharp
07-02-2006, 09:09 AM
you're passing a statement to the setTimeout, which is looking for a function

//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.

subhailc
07-02-2006, 10:15 AM
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...

limpsharp
07-02-2006, 10:55 AM
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 :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum