ignore document.onkeyup events for set a period?

P

paul.sherwood

Hi

Ive made a game for babies that simply displays one of an array of
pictures of animals along with there sound each time the keyboard is
'bashed'.

at the moment i dynamically load pictures using

<img id="image" src="somepic.gif" alt="" border=0px/>
<script type="text/javascript">
document.onkeyup=changeImageSrc;
</script>

and

function changeImageSrc(){
..
..
document.getElementById('image').src = nextImage; //nextImage is
already loaded from the image array
}

This works great for adults. However babies dont tend to wait for the
image and sound to have played before bashing again, so lots of keyup
events come along and the page ends up being a blur of images and
sounds. How might i best remedy this issue?

thanks in advance

Paul
 
E

Evertjan.

wrote on 27 jan 2006 in comp.lang.javascript:
Hi

Ive made a game for babies that simply displays one of an array of
pictures of animals along with there sound each time the keyboard is
'bashed'.

at the moment i dynamically load pictures using

<img id="image" src="somepic.gif" alt="" border=0px/>
<script type="text/javascript">
document.onkeyup=changeImageSrc;
</script>

and

function changeImageSrc(){
.
.
document.getElementById('image').src = nextImage; //nextImage is
already loaded from the image array
}

This works great for adults. However babies dont tend to wait for the
image and sound to have played before bashing again, so lots of keyup
events come along and the page ends up being a blur of images and
sounds. How might i best remedy this issue?

Ideas:

1 Prevent the changeImageSrc() execution for a fixt time with setTimeout()

2 Use setTimeut() to wait till the babies are older.

3 Preload the images.
 
R

Randy Webb

(e-mail address removed) said the following on 1/27/2006 11:26 AM:
Hi

Ive made a game for babies that simply displays one of an array of
pictures of animals along with there sound each time the keyboard is
'bashed'.

at the moment i dynamically load pictures using

<img id="image" src="somepic.gif" alt="" border=0px/>
<script type="text/javascript">
document.onkeyup=changeImageSrc;

var runTheFunction = true;
</script>
and
function changeImageSrc(){

if (runTheFunction){
runTheFunction = false;
..
document.getElementById('image').src = nextImage; //nextImage is
already loaded from the image array

runTheFunction = true;
}
 
P

paul.sherwood

Wait till they're older...chuckle

I have tried the setTimeout() function but unfortunately the browser
'remembers' all onkeyup events even during the wait period, which
causes the images to scroll by slower but still uncontrolled.
 
P

paul.sherwood

Unfortunatley the onkeyup events seem to be stored and executed after
the changeImageSrc function has completed, so multiple bashes still
result in multiple pages flashing past.
 
P

paul.sherwood

An alternative approach would be to some how ignore onkeyup events
until the animal sound has finished playing. I use

<bgsound id="sounds" src="sounds/default.wav"/> **i realise bgsound
only works for IE, but the trouble im having with that is a story for
another day


function changeImageSrc(){
..
..
document.getElementById('sounds').src = nextSound;
}

ive no idea how to do that. bgsound doesnt seem to have a
'stillPlaying' method.
 
E

Evertjan.

wrote on 27 jan 2006 in comp.lang.javascript:
Wait till they're older...chuckle

I have tried the setTimeout() function but unfortunately the browser
'remembers' all onkeyup events even during the wait period, which
causes the images to scroll by slower but still uncontrolled.

No, no, each keyup is processed, but with an "empty process"
till the next baby bounce is accepted again.

Try:

<body onkeyup='doit()'>

<script ...
var wait = false;

function doit(){

if (wait) return;

wait = true;
setTimeout('wait=false',1000);

// and do whatever you were up to originally

}
 
P

paul.sherwood

Thanks Evertjan, the above works splendidly. Suddenly its all become
clear.

Paul
 
E

Evertjan.

wrote on 31 jan 2006 in comp.lang.javascript:
Thanks Evertjan, the above works splendidly. Suddenly its all become
clear.

There is nothing "above". ;-)

==========================

Please, for the sake of the NG, quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at the
top of the article, then click on the "Reply" at the bottom of the article
headers. <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top