473,387 Members | 1,899 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,387 software developers and data experts.

Force update of on screen render after DOM element change

I know this question comes up from time to time, but 10-15 minutes of
googling hasn't produced a useful answer for me. I'm looking for the
equivalent of insert_magic_here() in:

some_element.style.color = 'red';
insert_magic_here();
some_element.style.color = 'blue';
insert_magic_here();

where insert_magic_here() will cause the updated DOM to be rendered
immediately. I need to do some animation in the middle of an event
handler (keypress). Is the basic problem that Firefox doesn't do the
on screen render until the event handler returns? Or ...? Anyway there
is quite a bit of work to do after this completes, including,
potentially, many other similar sequences of events.

If it helps, this code is intended to run only in JavaScript >= 1.7,
with Firefox as a reference platform.

I can come up with various painful ways to do this with closures and
so on but I am looking for something that won't serve as an entry in
an obfuscated Javascript contest. Also I haven't looked at generators
as a solution yet.

Your Help Is Appreciated(TM).
Jun 27 '08 #1
5 3610
Continuing, the simplest approach I've come up with, so far, is to
accumulate drawing requests in an Array as I handle the event,
with each object in the array specifying stuff that has to be
drawn atomically and a delay to be applied before the next
drawing happens, and then schedule all that to be processed after
the event handler returns. This would be fairly efficient and
possibly not drive the garbage collector crazy. I still have
to block receipt/processing of the next keypress until that
completes, though.

On May 16, 10:00 am, joebloe <remid0d...@gmail.comwrote:
I know this question comes up from time to time, but 10-15 minutes of
googling hasn't produced a useful answer for me. I'm looking for the
equivalent of insert_magic_here() in:

some_element.style.color = 'red';
insert_magic_here();
some_element.style.color = 'blue';
insert_magic_here();

where insert_magic_here() will cause the updated DOM to be rendered
immediately. I need to do some animation in the middle of an event
handler (keypress). Is the basic problem that Firefox doesn't do the
on screen render until the event handler returns? Or ...? Anyway there
is quite a bit of work to do after this completes, including,
potentially, many other similar sequences of events.

If it helps, this code is intended to run only in JavaScript >= 1.7,
with Firefox as a reference platform.

I can come up with various painful ways to do this with closures and
so on but I am looking for something that won't serve as an entry in
an obfuscated Javascript contest. Also I haven't looked at generators
as a solution yet.
Jun 27 '08 #2
On Fri, 16 May 2008 10:00:18 -0700, joebloe wrote:
I know this question comes up from time to time, but 10-15 minutes of
googling hasn't produced a useful answer for me. I'm looking for the
equivalent of insert_magic_here() in:

some_element.style.color = 'red';
insert_magic_here();
some_element.style.color = 'blue';
insert_magic_here();
Javascript is single threaded and (usually) runs in the same thread as
the window. As such, the window itself does not update while Javascript
is running. You must use one of the timeout features to allow the window
to refresh then start running the script again.
where insert_magic_here() will cause the updated DOM to be rendered
immediately. I need to do some animation in the middle of an event
handler (keypress).
Is the basic problem that Firefox doesn't do the on
screen render until the event handler returns?
Correct.
Or ...? Anyway there is
quite a bit of work to do after this completes, including, potentially,
many other similar sequences of events.

If it helps, this code is intended to run only in JavaScript >= 1.7,
with Firefox as a reference platform.
Because of the highly dynamic nature of the software, and web browsers in
particular, I would hesitate to come up with any solution that only
worked in any single User Agent.
>
I can come up with various painful ways to do this with closures and so
on but I am looking for something that won't serve as an entry in an
obfuscated Javascript contest. Also I haven't looked at generators as a
solution yet.
Depending on the exact nature of your code, you might be able to use a
single global object to store important values from one execution to the
next.
Your Help Is Appreciated(TM).
Jun 27 '08 #3
On May 16, 1:06 pm, Jeremy J Starcher <r3...@yahoo.comwrote:
On Fri, 16 May 2008 10:00:18 -0700, joebloe wrote:
I know this question comes up from time to time, but 10-15 minutes of
googling hasn't produced a useful answer for me. I'm looking for the
equivalent of insert_magic_here() in:
some_element.style.color = 'red';
insert_magic_here();
some_element.style.color = 'blue';
insert_magic_here();

Javascript is single threaded and (usually) runs in the same thread as
the window. As such, the window itself does not update while Javascript
is running. You must use one of the timeout features to allow the window
to refresh then start running the script again.
I was aware that on at least some level (window, document, browser,
whatever) the execution model for JS is single threaded, but I wasn't
aware that rendering thread == execution thread. Well, okay, that
makes much more sense then. I'm going to give my "logic first, render
second" approach a try; should work fine but I hope there's not too
much memory management churn as a result. In other words, the stuff
that is animated, I'll generate a sequence of actions for, then
execute those with a series of timeout events.
If it helps, this code is intended to run only in JavaScript >= 1.7,
with Firefox as a reference platform.

Because of the highly dynamic nature of the software, and web browsers in
particular, I would hesitate to come up with any solution that only
worked in any single User Agent.
What I'm working on doesn't need to be portable in the short term; if
a year goes by and no more 1.7 support is on the horizon, then I'll
fix it for whatever else is available at that time. But for now you
will pry my 'let' from my cold, dead hands.
Depending on the exact nature of your code, you might be able to use a
single global object to store important values from one execution to the
next.
That won't really fly; there are 1000s of lines of code that have to
be executed serially per keypress event.

This is an oddball application; I'll drop a URL later if it gets
anywhere.
Jun 27 '08 #4
Well this was entertaining to solve. I wound up postponing my drawing
until after the keypress event handler, then drawing a bit at a time
with timeout events. Had to disable the keypress handler while that
was going on; once I did that, each separate bit was rendered after
its timeout function returned. It's a little weird to wrap your head
around at first, but it works pretty well.
Jun 27 '08 #5
On Mon, 19 May 2008 10:42:45 -0700, joebloe wrote:
Well this was entertaining to solve. I wound up postponing my drawing
until after the keypress event handler, then drawing a bit at a time
with timeout events. Had to disable the keypress handler while that was
going on; once I did that, each separate bit was rendered after its
timeout function returned. It's a little weird to wrap your head around
at first, but it works pretty well.
You've got me curious now.

Care to share a URL?
Jun 27 '08 #6

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

Similar topics

2
by: Lo?c Mah? | last post by:
Hello I have a problem to update the content of a wx.Panel already displayed after changing a variable, used to define the wx.Panel content. I have the following elements in my code: ...
6
by: charly | last post by:
Greetings, I'm trying my hand at xml and css (no, I don't want to use XSL yet). So I got a test.xml : <?xml version="1.0" encoding="ISO-8859-1" ?> <?xml-stylesheet href="styles.css"...
3
by: xscully | last post by:
Is there a way to force the user to read a textarea? Just like those 'agreements' that is used in a few offline instalation.. You have to scroll all the way down of the textarea and then the I...
3
by: buht | last post by:
Hello Everyone, Fairly new to c# here and have a question regarding scrollbars, particularly a vertical scrollbar. It looks like my options are restricted for the textbox scrollbars being...
2
by: Faraz | last post by:
Hi, I am working with DOM and I need to do the following: <sequence> <element name="input1" type="string"/> <element name="input2" type="string"/> <element name="input3" type="string"/>...
5
by: sk | last post by:
Hi, I'm trying to override the Render method of my Page class. Are there any standard guidelines for this? Thanks. Shardul
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
1
by: RICHARD BROMBERG | last post by:
I am using the following code to display the total of the amount field in the Position table. Private Sub txtTotal_Click() Dim curX As Long curX = DSum("", "position") txtTotal.Text =...
4
by: AAaron123 | last post by:
<body runat="server" id="MainBody"> <form id="form1" runat="server" style="background-color:green; width: 100%; height: 100%"> <br /> Table1" runat="server" Style="background-color:Yellow;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.