Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes HTML, CSS and JavaScript and the fly likes why setTimeout is needed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "why setTimeout is needed?" Watch "why setTimeout is needed?" New topic

why setTimeout is needed?

Leena Diwan
Ranch Hand

Joined: Jun 18, 2001
Posts: 351
Real Question
1. Why do we need such timeouts in Javascript?
2. Is there any standard and elegant workaround for such timeouts?
3. What causes these inconsistencies because of timeout not put? Is that the order of the js files being downloaded or anything else?

If you need the background is like the following
We have a grid solution in place for the HTML tables.

This HTML table can be resized, some columns can be made hidden, columns can be dragged and dropped to change their ordering.

This all is implemented using javascript. Also AJAX request is fired on unload of the page so that whatever changes the user has made can
be stored in session. If user visits the same page again, then these settings from the session can be "reapplied" and user does not have to do it again in the same session.

This reapplication is done like the following - we paint all the HTML table and then on top of that we apply users settings from session, like hide this column and make another wider etc.
This is all working fine.

Now we wanted to introduce "default settings" for particular pages.
Like less needed columns can be default hidden and columns like description can be default wider and so on.

What we did was to read the default settings from a properties file and add them in the session when user first vists the page.
So that our "reapplying" logic will pick the settings [we save them in a hidden variable on the page] and next working will be as it is in the above case.

But the settings are not getting applied for no apparent reason.

We do not get a JS error or anything.

The wierd this is that if we add one alert somewhere, the issue disappears and the default settings are applied properly.
If we remove the alert, it does not work.

We figured this is a delay issue and added some delay using javascripts setTimeout method. It works fine when delay is applied.
But it is taking delay in seconds when onsite environment is accessed. Locally it runs with delay of about 1 ms.

Value your each suggestion. Thanks in advance!


Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
You are probably calling the code before it is ully rendered. You probably should be doing it on page load or the very last step before the body tag.

Leena Diwan
Ranch Hand

Joined: Jun 18, 2001
Posts: 351
Hey Eric..

found the error.. there was one onload function in some other JS that was overriding the settings.. since it was onload, it got called whatever else we tried to twick from the regular flow of method calls.

Thank you for you reply.

I agree. Here's the link:
subject: why setTimeout is needed?
It's not a secret anymore!