473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Window repaint/redraw delay

I have some code like:

try {
someButton.disabled = true;
cpuIntensiveCode();
} finally {
someButton.disabled = false;
}

The problem is that someButton is never disabled, because the browser
is busy thinking in cpuIntensiveCode. Indeed, if I don't reenable
someButton in the finally part, it waits until cpuIntensiveCode is over
to disable someButton.

The only way to disable someButton before cpuIntensiveCode is to force
the browser to redraw the window (eg: by putting an alert(...) just
after someButton is disabled).

I don't want a useless alert popping every time I run cpuIntensiveCode.
Does someone know a way to force the redraw of the browser window (not
reloading the page, just redrawing it)?

Apr 19 '06 #1
2 13044
di*****@gmail.com said the following on 4/19/2006 3:32 PM:
I have some code like:

try {
someButton.disabled = true;
cpuIntensiveCode();
} finally {
someButton.disabled = false;
}
As long as you know the drawbacks and problems with try/catch/finally.
The problem is that someButton is never disabled, because the browser
is busy thinking in cpuIntensiveCode.
It is disabled, it just hasn't changed view on the browser window.
Indeed, if I don't reenable someButton in the finally part, it waits
until cpuIntensiveCode is over to disable someButton.
No, it waits until it is completed to continue execution at which time
it would show the disabled button even though the button was disabled to
start with.

The only way to disable someButton before cpuIntensiveCode is to force
the browser to redraw the window (eg: by putting an alert(...) just
after someButton is disabled).
Read above. Use a settimeout and you will see the difference:

try {
someButton.disabled = true;
window.setTimeout(cpuIntensiveCode,100);
} finally {
someButton.disabled = false;
}

But, why are you even using try/finally there?
I don't want a useless alert popping every time I run cpuIntensiveCode.
Then don't use one.
Does someone know a way to force the redraw of the browser window (not
reloading the page, just redrawing it)?


See above.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 19 '06 #2
On 2006-04-19 22:35:44 +0200, Randy Webb <Hi************@aol.com> said:
di*****@gmail.com said the following on 4/19/2006 3:32 PM:
I have some code like:

try {
someButton.disabled = true;
cpuIntensiveCode();
} finally {
someButton.disabled = false;
} The problem is that someButton is never disabled, because the browser
is busy thinking in cpuIntensiveCode.


It is disabled, it just hasn't changed view on the browser window.


actually, it is not disabled, but neither will the browser acknowledge
any user activity while the code is running. It may remember any clicks
and later call any callback methods that should have been called, of
forget them.
Indeed, if I don't reenable someButton in the finally part, it waits
until cpuIntensiveCode is over to disable someButton.


No, it waits until it is completed to continue execution at which time
it would show the disabled button even though the button was disabled
to start with.


Actually, it seems the behavior of interface elements is not updated
much sooner than their graphical display. If a button is showed as
disabled you cannot click it, even if you just fired some code which
enables it but that code is not completed. That may depend on browsers
though. I can't test with IE.
The only way to disable someButton before cpuIntensiveCode is to force
the browser to redraw the window (eg: by putting an alert(...) just
after someButton is disabled).


Read above. Use a settimeout and you will see the difference:

try {
someButton.disabled = true;
window.setTimeout(cpuIntensiveCode,100);
} finally {
someButton.disabled = false;
}


Browsers don't update the display while the code that initiated the
updates is still running.

so, setTimeout is indeed the way to go, because it creates a sort of
"thread" that may execute very quickly and as soon as it's done, the
browser will redraw whatever was affected by that thread.

But the above example won't work as intended. The "CPU intensive code"
will be executed in a separate thread, completely outside of the try()
statement. So, yes, the button will be disabled immediately, but it
will also be re-enabled after 100ms, whether the CPU intensive code is
completed or not (only the setTimeout call must be completed, which
shouldn't take too long).

What you should do is the opposite :

try {
window.setTimeout("someButton.disabled = true", 1)
cpuIntensiveCode
} finally {
someButton.disabled = false
}

What this code does is detach a small thread that will execute almost
immediately (you may not want to rely on the button being unable to
intercept another onclick event, since many things can happen during
one millisecond) and change a property of the button. Since that thread
will be completed, the browser will redraw the button and handle user
events accordingly.
Meanwhile, the main thread will compute your intensive code, and won't
re-enable the button until that code is finished, since the re-enabling
code is part of the same thread.

Apr 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Altramagnus | last post by:
I have 30 - 40 type of different window. For each type I need about 20 instances of the window. When I try to create them, I get "Error creating window handle" My guess is there is a maximum...
1
by: John | last post by:
How to draw anmation in window app? I mean... like how can I draw a dot in window at a point then moves to another point and we can see it moves. Please explain in detail.
0
by: Víctor | last post by:
Hello, I'm doing a Intranet application which shows some information panels dinamically. Each panel is a dll loaded dinamically using Reflection and inherits from Panel class. To display...
1
by: Diogo Alves - Software Developer | last post by:
Hi there, Since my last questions wasn't very clear, I will do it in a different way. I want to resize my app but while resizing I dont want to see content of the form, I just want to see the...
4
by: astromac | last post by:
I'm new to php and was wondering if the following was possible... I would like to have a list of items loaded from a text file, process each item in the list and then return the processed result...
2
by: Tony | last post by:
Hi, In a form, when I change the LitterLine field, I programmatically update the PupLine field, (as in the sample code below). However, the PupLine field only gets updated the first time after...
7
by: Paul | last post by:
I have a VB.NET form with a DataGrid. When I toggle to Excel (for example) and then back to my application the repaint of the DataGrid is really slow. You can see the repainting happening. When...
3
by: linuxadmin | last post by:
hello! i want to be able to repaint a class, derived from a form, by myself. it works automatically, when i do: protected override void OnPaint(PaintEventArgs e){ base.OnPaint(e); Graphics...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.