473,387 Members | 1,291 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Error in JS?

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


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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Gregory | last post by:
Hi, One of the disadvantages of using error handling with error codes instead of exception handling is that error codes retuned from a function can be forgotten to check thus leading to...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
3
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
5
by: yashu0209 | last post by:
I have an error occurring when i try to compile the program and the error is: D:\Program Files\visual studio 6.0\MSDev98\MyProjects\GLEnabledView\GLNew.cpp(164) : error C2653: 'CGLEnabledView' :...
2
by: randa zaghdan | last post by:
Hi I have a problem with my program in vc 2008 When I compile it, the following errors are listed. I try to resolve it but no hope Does any one can help me ? thanks . Linking......
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.