Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript re-write of js/php music composition program
    primarykey
    data
    text
    <p>A while ago, I created (with much help) a program that uses php and javascript to make some ambient music by randomizing an array of <code>.ogg</code> files. Now I am trying to re-write it with js alone.</p> <p>The new code immediately below does <em>not</em> work (the <code>.ogg</code> files do play properly when clicked individually, but nothing happens when you press the 'start' button, which is the main feature). (The code below that (containing php) <em>does</em> work.) I've been over the new code several times and I think I've got the 'typos'. I'm pretty sure the problem is in the syntax of the window.setTimeout line, but haven't quite figured out how it should be.</p> <p>Thanks for any help you can offer!</p> <pre><code>&lt;!DOCTYPE html&gt; &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;script&gt; function getRandInt (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function shuffle(o) { for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o; } &lt;/script&gt; &lt;script type="text/javascript"&gt; var tonesTotal = 150; function getDelays(tonesTotal) { var return_array = array(); for (var i = 0; i &lt; tonesTotal; i++) { var r = getRandInt(0, 600); var delay = r * 1000; return_array.push(delay); } return return_array; } var delays = new Array(); delays = getDelays(tonesTotal); $(document).ready(function() { $("#start").click(function() { var base = 'sound'; for(var i = 0; i &lt; tonesTotal; i++) var id = base + ((i + 1) ); window.setTimeout ("document.getElementById('" + id + "').play()", delays[i]); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body style="background-color: #999;"&gt; &lt;button id="start"&gt;Start&lt;/button&gt; &lt;br&gt;&lt;br&gt; &lt;script&gt; var tonesTotal = 150; var samples = new Array("tone0.ogg", "tone2.ogg", "tone4.ogg", "tone6.ogg", "tone7.ogg", "tone9.ogg", "tone11.ogg", "tone12.ogg"); for (var i=1; i &lt;= tonesTotal; i++) { shuffle (samples); var samplepick = samples[0]; document.write ("&lt;audio controls id='sound" + i + "'&gt;&lt;source src='" + samplepick + "' type='audio/ogg'&gt;&lt;/audio&gt;"); } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>PREVIOUS CODE:</p> <pre><code>&lt;!DOCTYPE html&gt; &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(1,4); $C = 150; function getDelays($C) { $return_array = array(); for($i = 0; $i &lt; $C; $i++) { $r = rand(0, 600); $delay = $r * 1000; array_push($return_array, $delay); } return $return_array; } echo "&lt;script type=\"text/javascript\"&gt;\n"; echo "var delays = new Array();"; $delays = getDelays($C); for($i = 0; $i &lt; sizeof($delays); $i++) { echo "delays[" . $i . "] = " . $delays[$i] . ";\n"; } echo " $(document).ready(function() { $(\"#start\").click(function() { var base = 'sound'; for(i = 0; i &lt; $C; i++) { var id = base + ((i + 1) ); window.setTimeout (\"document.getElementById('\" + id + \"').play()\", delays[i]); } }); }); &lt;/script&gt; "; ?&gt; &lt;style type="text/css"&gt; &lt;/style&gt; &lt;/head&gt; &lt;body style="background-color: #999;"&gt; &lt;button id="start"&gt;Start&lt;/button&gt; &lt;br&gt;&lt;br&gt; &lt;?php $samples = array("tone0.ogg", "tone2.ogg", "tone4.ogg", "tone6.ogg", "tone7.ogg", "tone9.ogg", "tone11.ogg", "tone12.ogg"); for ($i=1; $i&lt;= $C; $i++) { shuffle ($samples); $samplepick = $samples[0]; echo '&lt;audio controls id="sound'.$i.'"&gt; &lt;source src='.$samplepick.' 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.
 

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