javascript - 一次仅显示数据库中的一项,并按时间周期循环显示其他项

标签 javascript php jquery sql ajax

我有一个项目,要在我办公室的主屏幕上显示一些详细信息,但问题是我只想一次显示项目,然后在时间段内循环显示每个项目。

下面是我的 php、Java Sciptis 和索引页。

index.html

<ul></ul>
<script type="text/javascript" src="script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="script/my_script.js"></script>

获取.php

<?php
        include_once('db.php');

        $sql = "SELECT * FROM employee";
        $res = mysql_query($sql);
        $result = array();

        while( $row = mysql_fetch_array($res) )
            array_push($result, array('ID' => $row[0],
                                      'Name'  => $row[1],
                          'Post' => $row[2]));

        echo json_encode(array("result" => $result));
?>

my_script.js

$(document).ready( function(){
done(); 

});

function done() {
    setTimeout(function() {
        updates();
        done();
        },200);

}

function updates() {
$.getJSON("fetch.php", function(data) {
    $("ul").empty();

    $.each(data.result,function(){
        $("ul").append("<li>ID: "+this['ID']+"</li><li>Name: "+this['Name']+"</li><li>Designation: "+this['Post']+"</li><br />");
    });
}); 
}

这个脚本不是我写的。所以我需要一些帮助由于上面的脚本将显示项目而不刷新它,我想一​​次只显示一个项目。

谢谢!!!

最佳答案

根据您的评论,我认为这对您有用。它在页面加载时获取一次数据,然后无限循环(当到达数据末尾时再次从头开始)。

var i = 0; // index of data to iterate.
var d = null; // where we store the result of the query.
var interval = 1000; // interval in ms.

$(document).ready(function()
{
    // get the data *once* when the page loads.
    $.getJSON('fetch.php', function(data)
    {
        // store the data in the global var 'd'.
        d = data.result;

        // create a recurring call to update().
        setInterval(function()
        {
            update();
        },
        interval);
    });
});

function update()
{
    // if there isn't another element, reset to the first one.
    if (!d[i]) i = 0;

    // remove previous item from page.
    $('ul').empty();

    // add next item to page.
    $("ul")
        .append(
            '<li>ID: ' + d[i]['ID']
            + '</li><li>Name: ' + d[i]['Name']
            + '</li><li>Designation: ' + d[i]['Post']
            + '</li>'
        );

    // increment index for next iteration.
    i++;
}
<小时/>

到达最后一条记录后重新获取的替代版本

var i = 0, // index of data to iterate.
    d = null, // where we store the result of the query.
    x = null, // stored interval so clearInterval() can be used.
    interval = 1000; // interval in ms.


$(document).ready(function()
{
    fetch();
});

function fetch()
{
    // get the data *once* when the page loads.
    $.getJSON('fetch.php', function(data)
    {
        // store the data in the global var 'd'.
        d = data.result;

        // create a recurring call to update().
        x = setInterval(function()
        {
            update();
        },
        interval);
    });
}

function update()
{
    // if there isn't an array element, reset to the first one.
    if (!d[i]) {
        clearInterval(x);
        i = 0;
        fetch();
        return;
    }

    // remove previous item from page.
    $('ul').empty();

    // add next item to page.
    $("ul")
        .append(
            '<li>ID: ' + d[i]['ID']
            + '</li><li>Name: ' + d[i]['Name']
            + '</li><li>Designation: ' + d[i]['Post']
            + '</li>'
        );

    // increment index for next iteration.
    i++;
}

关于javascript - 一次仅显示数据库中的一项,并按时间周期循环显示其他项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24713317/

相关文章:

javascript - 使用 jQuery 反向滚动元素

javascript - babylon.js 网格获得相同的 Material

php - 使用 Visual Studio 在 IIS Express 上启用 PHP(无 WebMatrix)

php crontab -l 从命令行显示不同的结果

php - 通过 PHP 解析日志文件

javascript - 数据表.net : Change theme of grid

javascript - 为什么加载新的 html 页面会重置 javascript 变量?我如何保存这些变量?

javascript - 在 jQuery 中循环数组来更改 CSS

javascript - 使用 React Native 进行网络抓取和注入(inject) JavaScript 的最佳方式?

jquery - 需要从选定的复选框中获取值并将其放入输入文件中,并用逗号分隔