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
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