470,602 Members | 1,646 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,602 developers. It's quick & easy.

How to blink once?

kj


I would like to briefly highlight a portion of the page in response
to certain mouse click. In pseudo-code, my code is something like
this:

function handle( event ) {
var chunk_o_text = get_chunk( event );

// reveal chunk_o_text
chunk_o_text.style.visible = true;

var subchunk = get_subchunk( event, chunk_o_text );

// briefly highlight subchunk
highlight( subchunk, 'on' );
sleep( 250 ); // a 250 ms delay
highlight( subchunk, 'off' );

return;
}

where the highlighting is implemented by messing around with the
class name, e.g.:

function highlight( elem, on ) {
var cn = elem.className;
if ( on == 'on' ) {
elem.className = cn + ' highlighted';
}
else {
elem.className = cn.replace( /\bhighlighted\b/g, '' );
}
}

....and, of course, there's also a corresponding suitable entry in the
stylesheet:

.highlighted {
color: #fff;
background: #000;
}

This scheme does not have the desired effect: the user never sees
the temporary highlighting of subchunk. I know the highlighting
works, because if I comment out the "highlight( subchunk, 'off' )"
statement in handle(), then subchunk becomes (permanently) highlighted
after the mouse click.

I've tried a *bazillion* variants on this theme. All fail, because
it appears that the entire sequence of statements in handle() gets
played out before the GUI is redrawn, so the end result always
shows the final state, in which the highlighting is off. I have
also tried various silly tricks to force a redraw in-between the
highligh-on and highligh-off statements, but none have worked.

I'm sure I'm reinventing a very old wheel here. What's the right
way to do what I'm trying to do?

Thanks!

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Dec 6 '06 #1
2 5362
On Thursday 07 December 2006 00:05, kj [so***@987jk.com.invalid] wrote in
message <el**********@reader2.panix.com>
I would like to briefly highlight a portion of the page in response
to certain mouse click. In pseudo-code, my code is something like
this:
highlight( subchunk, 'on' );
sleep( 250 ); // a 250 ms delay
highlight( subchunk, 'off' );
This scheme does not have the desired effect: the user never sees
the temporary highlighting of subchunk. I know the highlighting
works, because if I comment out the "highlight( subchunk, 'off' )"
statement in handle(), then subchunk becomes (permanently) highlighted
after the mouse click.
As you've found out, sleep doesn't stop execution of the script. Instead,
try the following:

highlight(subchunk, 'on');
setTimeout(highlight(subchunk, 'off'),250);
Dec 7 '06 #2
kj
In <45776616.0@entanetPDannyD <da*****@scenicplace.freeserve.co.ukwrites:
>On Thursday 07 December 2006 00:05, kj [so***@987jk.com.invalid] wrote in
message <el**********@reader2.panix.com>
>I would like to briefly highlight a portion of the page in response
to certain mouse click. In pseudo-code, my code is something like
this:
> highlight( subchunk, 'on' );
sleep( 250 ); // a 250 ms delay
highlight( subchunk, 'off' );
>This scheme does not have the desired effect: the user never sees
the temporary highlighting of subchunk. I know the highlighting
works, because if I comment out the "highlight( subchunk, 'off' )"
statement in handle(), then subchunk becomes (permanently) highlighted
after the mouse click.
>As you've found out, sleep doesn't stop execution of the script. Instead,
try the following:
>highlight(subchunk, 'on');
setTimeout(highlight(subchunk, 'off'),250);
Yes! setTimeout was the answer. Thanks!

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Dec 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Justin Sane | last post: by
2 posts views Thread by Ian Frawley | last post: by
6 posts views Thread by Marco | last post: by
reply views Thread by =?Utf-8?B?QnJpYW5ESA==?= | last post: by
26 posts views Thread by Rick | last post: by
1 post views Thread by Pradyumna | last post: by
7 posts views Thread by semomaniz | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.