Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript/jquery start button for .ogg looped play
    primarykey
    data
    text
    <p>The code below generates and loops a randomized number of iterations of some .ogg files. They play as soon as the page loads. </p> <p>The commented-out bits are my attempt to add a 'start' button. What am I doing wrong here, and how can I start this with a button rather than on page load?</p> <p>(Also: I am suspecting that it is bad practice to combine php and javascript as I have done. I did it because randomization in php is so easy, and I am still new to js/jquery.) </p> <p>(EDIT: see the edit below. I just forgot to close the tag.)</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Audio Testing&lt;/title&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"&gt;&lt;/script&gt; &lt;?php $randC = rand(2,4); $C = $randC * 4; $G = $C / 2; echo " &lt;script type=\"text/javascript\"&gt; $(document).ready(function() { // $(\"#start\").click(function() // { var time = 0; var riff_time = 2920; var base = 'riff'; for(i = 0; i &lt; $C; i++) { var id = base + ((i + 1) ); window.setTimeout (\"document.getElementById('\" + id + \"').play()\", time); time += riff_time; } var timeG = 0; var riff_time = 2920; var baseG = 'riffG'; for(iG = 0; iG &lt; $G; iG++) { var idG = baseG + ((iG + 1) ); window.setTimeout (\"document.getElementById('\" + idG + \"').play()\", timeG); timeG += riff_time; // } } // EDIT: This should be }); ... that fixes it. }); &lt;/script&gt; "?&gt; &lt;style type="text/css"&gt; &lt;/style&gt; &lt;/head&gt; &lt;body style="background-color: #999;"&gt; &lt;!--&lt;button id="start"&gt;Start&lt;/button&gt; &lt;br&gt; --&gt; &lt;?php echo "C:&lt;br&gt;"; for ($i=1; $i&lt;= $C; $i++) { echo '&lt;audio controls id="riff'.$i.'"&gt; &lt;source src="loopC.ogg" type="audio/ogg"&gt; &lt;/audio&gt;'; } echo "&lt;br&gt;G:&lt;br&gt;"; for ($i=1; $i&lt;= $G; $i++) { echo '&lt;audio controls id="riffG'.$i.'"&gt; &lt;source src="loopG.ogg" type="audio/ogg"&gt; &lt;/audio&gt;'; } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload