Javascript重写js / php音乐创作程序

时间:2013-08-31 16:27:29

标签: php javascript

不久前,我创建了一个程序,使用php和javascript通过随机化.ogg文件数组来创建(有很多帮助)程序。现在我试图用js单独重写它。

下面的新代码工作(.ogg个文件在单独点击时确实可​​以正常播放,但按下'开始'按钮时没有任何反应,这是主要功能)。 (下面的代码(包含php) 工作。)我已经多次使用新代码,我认为我有'拼写错误'。我很确定问题出在window.setTimeout行的语法中,但还没弄清楚它应该是怎么回事。

感谢您提供的任何帮助!

<!DOCTYPE html>
<html>
<head>

<title>Audio Testing</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>
    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;
        }

</script>

<script type="text/javascript">

    var tonesTotal = 150;  

    function getDelays(tonesTotal)   
        {
            var return_array = array();
            for (var i = 0; i < 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 < tonesTotal; i++)
                    var id = base + ((i + 1) ); 
                    window.setTimeout ("document.getElementById('" + id + "').play()", delays[i]);

                });
        }); 

</script>
</head> 

<body style="background-color: #999;">

    <button id="start">Start</button>
    <br><br>

<script> 
    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 <= tonesTotal; i++) 
    {
        shuffle (samples);
        var samplepick = samples[0];
        document.write ("<audio controls id='sound" + i + "'><source src='" + samplepick + "' type='audio/ogg'></audio>");
    }               
</script>
</body>
</html>

以前的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Audio Testing</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <?php 
    //$randC = rand(1,4); 
    $C = 150;

    function getDelays($C)
    {
        $return_array = array();
        for($i = 0; $i < $C; $i++)
        {
            $r = rand(0, 600);
            $delay = $r * 1000;
            array_push($return_array, $delay);
        }
        return $return_array;
    }
    echo "<script type=\"text/javascript\">\n";
    echo "var delays = new Array();";
    $delays = getDelays($C);
    for($i = 0; $i < sizeof($delays); $i++)
    {
        echo "delays[" . $i . "] = " . $delays[$i] . ";\n";
    }
    echo "
            $(document).ready(function() 
            {
              $(\"#start\").click(function() 
              { 
                var base = 'sound';
                for(i = 0; i < $C; i++)
                {           
                    var id = base + ((i + 1) ); 
                    window.setTimeout (\"document.getElementById('\" + id + \"').play()\", delays[i]);
                }
              });             
            });
        </script>
        ";
        ?>

        <style type="text/css">
        </style>
</head> 

<body style="background-color: #999;">

    <button id="start">Start</button>
    <br><br>

    <?php
    $samples = array("tone0.ogg", "tone2.ogg", "tone4.ogg", "tone6.ogg", "tone7.ogg", "tone9.ogg", "tone11.ogg", "tone12.ogg"); 
    for ($i=1; $i<= $C; $i++) {
        shuffle ($samples);
        $samplepick = $samples[0];
        echo '<audio controls id="sound'.$i.'">
        <source src='.$samplepick.' type="audio/ogg">
        </audio>';
    }

    ?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

当我在本地测试它时,我在getDelays()函数中发现了一个Javascript错误:第一行代码应为:

var return_array = new Array();