Error in JS?

G

Grzegorz

Hi All,

I encountered recently the following strange behavior of JS:
I need to create a simple page that contains one button. After clicking it
there should be generated two short sounds with a 0,5s gap between them. The
code looks as follows:

<html>
<EMBED SRC="sound1.wav" LOOP=FALSE AUTOSTART=false HIDDEN=true>
<EMBED SRC="sound2.wav" LOOP=FALSE AUTOSTART=false HIDDEN=true>

<script language="JavaScript">

function PlaySounds() {
document.embeds[0].play();
for (j=1;j<=100000;j++) {a=Math.log(1)}
document.embeds[1].play();
}

</script>
<button onClick="PlaySounds()">Play sounds</button>
</html>

Please look at three rows of PlaySounds function. The for loop is used to
introduce 0,5 s delay, but depending on processor speed it may take longer
or shorter.
It seems like there should be emitted the first sound, then 0,5 silence and
second sound. As I experienced IT IS NOT SO! First there is the loop
executed and then both sounds mixed, emitted simultaneously!
Why???
And the second qestion, how to achieve desired effect? Please do not advice
me the setTimeout function because with long series of sound samples it
works improperly.

Regards,
Grzegorz
 
M

Martin Honnen

Grzegorz said:
I encountered recently the following strange behavior of JS:
I need to create a simple page that contains one button. After clicking it
there should be generated two short sounds with a 0,5s gap between them. The
code looks as follows:

<html>
<EMBED SRC="sound1.wav" LOOP=FALSE AUTOSTART=false HIDDEN=true>
<EMBED SRC="sound2.wav" LOOP=FALSE AUTOSTART=false HIDDEN=true>

<script language="JavaScript">

function PlaySounds() {
document.embeds[0].play();
for (j=1;j<=100000;j++) {a=Math.log(1)}
document.embeds[1].play();
}

</script>
<button onClick="PlaySounds()">Play sounds</button>
</html>

Please look at three rows of PlaySounds function. The for loop is used to
introduce 0,5 s delay, but depending on processor speed it may take longer
or shorter.
It seems like there should be emitted the first sound, then 0,5 silence and
second sound. As I experienced IT IS NOT SO! First there is the loop
executed and then both sounds mixed, emitted simultaneously!
Why???
And the second qestion, how to achieve desired effect? Please do not advice
me the setTimeout function because with long series of sound samples it
works improperly.

Use setTimeout. Your loop blocks the browser.
 
B

Brian Genisio

Grzegorz said:
Hi All,

I encountered recently the following strange behavior of JS:
I need to create a simple page that contains one button. After clicking it
there should be generated two short sounds with a 0,5s gap between them. The
code looks as follows:

<html>
<EMBED SRC="sound1.wav" LOOP=FALSE AUTOSTART=false HIDDEN=true>
<EMBED SRC="sound2.wav" LOOP=FALSE AUTOSTART=false HIDDEN=true>

<script language="JavaScript">

function PlaySounds() {
document.embeds[0].play();
for (j=1;j<=100000;j++) {a=Math.log(1)}
document.embeds[1].play();
}

</script>
<button onClick="PlaySounds()">Play sounds</button>
</html>

Please look at three rows of PlaySounds function. The for loop is used to
introduce 0,5 s delay, but depending on processor speed it may take longer
or shorter.
It seems like there should be emitted the first sound, then 0,5 silence and
second sound. As I experienced IT IS NOT SO! First there is the loop
executed and then both sounds mixed, emitted simultaneously!
Why???
And the second qestion, how to achieve desired effect? Please do not advice
me the setTimeout function because with long series of sound samples it
works improperly.

Regards,
Grzegorz

You are hearing them at the same time, likely, because the sound does
not actually play, until the javascript block has ended. The message to
play the sound is queued up, and when the block ends, it plays it... and
the next one.

There have only been, in my experience, a few situations where
busy-waits are appropriate when programming... and they have all been in
embedded systems, at the microprocessor level. In _ALL_ other cases,
the programming environment provided me with the resources to do a
non-blocking sleep.

In Javascript, it is setTimeout. I am not familiar with embedding
sounds, but you _might_ be able to use setTimeout to probe when the
sound has finished, and set the next timeout to play the next sound...
just a stab in the dark.

Brian
 

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
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top