This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

why setTimeout is needed?

Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Posts: 15385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic