在循环中调用 Same JS 函数以同时执行所有请求

我有一个 JS 函数,我试图从 PHP 代码中调用它。我调用这个 JS 函数循环运行的次数(例如 5)。代码:


    <script>

    //var seconds = '';

function csst(rowid){ alert(rowid);

    window.setInterval(function(){

        secondPassed();

        function secondPassed() {

        seconds = parseInt($("#ts").val());

        var minutes = Math.round((seconds - 30)/60);

        var remainingSeconds = seconds % 60;

        if (remainingSeconds < 10) {

            remainingSeconds = "0" + remainingSeconds; 

        }

        $("#countdown1"+rowid).html(minutes + ":" +    remainingSeconds);

        if (seconds <= 0) {

            clearInterval();

            document.getElementById('countdown1'+rowid).innerHTML = "Buzz Buzz...";

              } else {    

            seconds--;

            $("#ts").val(seconds);

        }

        }

},1000);


}


</script>

<?php

$c = 1;

echo '<input type="hidden" value="50" id="ts" name="ts"/>';


echo "<table border='1'>";

while ($c <=5)

{

    echo '<script>

        csst('.$c.'); 

        </script>';

    echo '<tr><td>row'.$c.'</td><td><p id="countdown1'.$c.'"></td></tr>';

    $c++;

}

echo "</table>";


?>

我需要在表格的每一行中同时显示一个时间递减计数器。使用相同的 JS 函数。


任何帮助,请!


暮色呼如
浏览 106回答 1
1回答

达令说

我以某种方式设法解决了这个问题。我只是想玩 pickup 变量echo '<input type="hidden" value="50" id="ts" name="ts"/>';将这段代码与在循环中创建的动态 ID 一起使用,同样在 JS 函数中动态选择值也有帮助。任何其他更好的方法总是好的。更新的工作代码:&nbsp; &nbsp; <script>&nbsp; &nbsp; //var seconds = '';function csst(rowid){ //alert(rowid);&nbsp; &nbsp; window.setInterval(function(){&nbsp; &nbsp; &nbsp; &nbsp; secondPassed();&nbsp; &nbsp; &nbsp; &nbsp; function secondPassed() {&nbsp; &nbsp; &nbsp; &nbsp; seconds = parseInt($("#ts"+rowid).val());&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var minutes = Math.round((seconds - 30)/60);&nbsp; &nbsp; &nbsp; &nbsp; var remainingSeconds = seconds % 60;&nbsp; &nbsp; &nbsp; &nbsp; if (remainingSeconds < 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remainingSeconds = "0" + remainingSeconds;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $("#countdown1"+rowid).html(minutes + ":" +&nbsp; &nbsp; remainingSeconds);&nbsp; &nbsp; &nbsp; &nbsp; if (seconds <= 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clearInterval();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('countdown1'+rowid).innerHTML = "Buzz Buzz...";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seconds--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#ts"+rowid).val(seconds);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }},1000);}</script><?php$c = 1;echo "<table border='1'>";while ($c <=5){$val = $c+50;echo '<input type="hidden" value="'.$val.'" id="ts'.$c.'" name="ts"/>';&nbsp; &nbsp; echo '<script>&nbsp; &nbsp; &nbsp; &nbsp; csst('.$c.');&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </script>';&nbsp; &nbsp; echo '<tr><td>row'.$c.'</td><td><p id="countdown1'.$c.'"></td></tr>';&nbsp; &nbsp; $c++;}echo "</table>";?>请分享任何其他解决方案。
打开App,查看更多内容
随时随地看视频慕课网APP