Javascript 与x php函数相同的JS函数

Javascript 与x php函数相同的JS函数,javascript,php,jquery,ajax,timer,Javascript,Php,Jquery,Ajax,Timer,我的php页面上有X个表。在每一个地方,我都有计时器。这些计时器的持续时间不同。我想知道如何使用一个JS/AJAX函数(单独)刷新这些计时器 php(我不能发布真正的代码,只是一个让您理解的示例) 您可以在每个表上使用数据属性,并通过ajax调用查询php脚本。下面是使用两个具有不同计时器计数的表的示例: HTML <div id="table1" class="container"> <table data-url="/path/to/script.php?table

我的php页面上有X个表。在每一个地方,我都有计时器。这些计时器的持续时间不同。我想知道如何使用一个JS/AJAX函数(单独)刷新这些计时器

php(我不能发布真正的代码,只是一个让您理解的示例)


您可以在每个表上使用数据属性,并通过ajax调用查询php脚本。下面是使用两个具有不同计时器计数的表的示例:

HTML

<div id="table1" class="container">
    <table data-url="/path/to/script.php?table=1" data-timer="1000">
        <!-- Table content -->
    </table>
</div>

<div id="table2" class="container">
    <table data-url="/path/to/script.php?table=2" data-timer="500">
        <!-- Table content -->
    </table>
</div>
PHP

$table = $_GET['table']

if($table == '1'){
    // ouput table 1 html
}else{
   // output table 2 html
}
我更改了代码:

JAVASCRIPT

// Here we select using the attribute, you could use a class or other
$('[data-timer]').each(function(){
    $this = $(this);
    $parent = $this.parents('div.container');

    // Set interval will call the load function every x milliseconds
    setInterval(function(){
        // Now we use the table data attributes to load the
        // correct data with the correct inteval
        $parent.load( $this.attr('data-url'), function() {
            console.log( "Load was performed." );
        });
    }, $this.attr('data-timer') );
});
function timer(i, start_date, duration) {
    setInterval(function() {
        var date_now = Math.round(new Date().getTime() / 1000);
        var rem_time = duration - (date_now - start_date);

        // Times calculs
        minuts = parseInt(rem_time / 60);
        seconds = parseInt(rem_time % 60);

        if (rem_time >= 0)
            document.getElementById("rem_time"+i).innerHTML = minuts + ":" + seconds;
    }, 1000);
}
PHP/HTML(混合但语法错误)



对于($i=1;$i因为我只想刷新html表的TD,而不是整个页面。单击答案附近的灰色勾号以验证它
$table = $_GET['table']

if($table == '1'){
    // ouput table 1 html
}else{
   // output table 2 html
}
function timer(i, start_date, duration) {
    setInterval(function() {
        var date_now = Math.round(new Date().getTime() / 1000);
        var rem_time = duration - (date_now - start_date);

        // Times calculs
        minuts = parseInt(rem_time / 60);
        seconds = parseInt(rem_time % 60);

        if (rem_time >= 0)
            document.getElementById("rem_time"+i).innerHTML = minuts + ":" + seconds;
    }, 1000);
}
<table>
    for ($i=1; $i<X; $i++) {
        <tr><td>
            <div id="rem_time".$i.> <script> timer($i, '14:30:25', 600);</script></div>
        </tr></td>
    }
</table>