Error in JS?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grzegorz

    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="JavaS cript">

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

    </script>
    <button onClick="PlaySo unds()">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


  • Martin Honnen

    #2
    Re: Error in JS?



    Grzegorz wrote:
    [color=blue]
    > 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="JavaS cript">
    >
    > function PlaySounds() {
    > document.embeds[0].play();
    > for (j=1;j<=100000; j++) {a=Math.log(1)}
    > document.embeds[1].play();
    > }
    >
    > </script>
    > <button onClick="PlaySo unds()">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.[/color]

    Use setTimeout. Your loop blocks the browser.

    --

    Martin Honnen


    Comment

    • HikksNotAtHome

      #3
      Re: Error in JS?

      In article <bs97mf$6id$1@n ews.onet.pl>, "Grzegorz" <grzeslaw@op.pl > writes:
      [color=blue]
      >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:[/color]
      [color=blue]
      ><script language="JavaS cript">[/color]

      use type="text/javascript" instead, language is deprecated.
      [color=blue]
      >function PlaySounds() {
      >document.embed s[0].play();
      >for (j=1;j<=100000; j++) {a=Math.log(1)}
      >document.embed s[1].play();
      >}
      >
      ></script>
      ><button onClick="PlaySo unds()">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.[/color]

      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.
      [color=blue]
      >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???[/color]

      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.
      [color=blue]
      >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.[/color]

      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

      Comment

      • Brian Genisio

        #4
        Re: Error in JS?

        Grzegorz wrote:
        [color=blue]
        > 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="JavaS cript">
        >
        > function PlaySounds() {
        > document.embeds[0].play();
        > for (j=1;j<=100000; j++) {a=Math.log(1)}
        > document.embeds[1].play();
        > }
        >
        > </script>
        > <button onClick="PlaySo unds()">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
        >
        >[/color]

        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

        Comment

        Working...