By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,567 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,567 IT Pros & Developers. It's quick & easy.

Error in JS?

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a


Grzegorz wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

P: n/a
In article <bs**********@news.onet.pl>, "Grzegorz" <gr******@op.pl> writes:
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: <script language="JavaScript">
use type="text/javascript" instead, language is deprecated.
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>

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.
No, you think its introducing a 1/2 second delay, but as you say, it doesn't
work. Thats not how you do it in javascript.
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???
Because too many other things can affect the speed at which it happens. My IE6
executes it the same way. But my processor is fast. On a 386 processor, it
would run slower.
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.


If you do not want to use the proper way to introduce a delay, your only other
option would be to edit the sound files and make a new file, with the delays
built in.

Or, use a blank sound file (of 1/2 second length) and then instead of embedding
the sounds, embed an .m3u file instead and play it.
--
Randy
Jul 20 '05 #3

P: n/a
Grzegorz wrote:
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

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.