运行 AJAX 每 x 秒,每次都有不同的数据

Run AJAX every x seconds with different data each time

我有下面的代码,每 5 秒执行一次。但是我想每次 return 不同的数据。

例如:

First interval = top level data (level.php)
Second interval = top skill data (skill.php)
Third interval = top magic data (magic.php)

一旦第三个间隔完成...return到level.php再次开始序列

有人能告诉我如何修改代码来实现我想要的吗?

<script>
    var text = "";
    var toplevel = function() {
        $.ajax({
            type : 'GET',
            url : '/pages/level.php',
            success : function(data){
            text = data;
            $("#tops").fadeOut( "normal", function() {
                $('#tops').html(data);
                $("#tops").fadeIn( "normal", function() {});
                });
            },
        });
    };

    $(document).ready(function(){
        setInterval(toplevel, 5000);
        toplevel();
    });
</script>

您可以有一个 URL 数组,始终将请求发送到第一个,并在成功时轮换数组顺序:

function toplevel(urls) {
    return function () {
        $.get(urls[0]).done(function (data) {
            urls.push(urls.shift());
            $("#tops").fadeOut("normal", function () {
                $('#tops').html(data).fadeIn( "normal");
            });
        });
    }
};

$(function () {
    var switcher = toplevel(['/pages/level.php', '/pages/skill.php', '/pages/magic.php']);
    setInterval(switcher, 5000);
    switcher();
});