jquery setInterval和setTimeout不工作,不循环



我在setTimeout方面遇到了问题。它根本没有循环。我也试过setInterval。我正试图循环浏览一个部署脚本,看看我们目前是否有任何代码正在部署。我们希望它每秒钟都能运行。它检查每一行,以查看部署是否正在进行中或是否已完成。我不知道我做错了什么。

$(document).ready(function()
{
    var file = "secret/dont/tell/anyone";
    setTimeout(function()
    {
        console.log('it was ran!');
        $.ajax(
        {
            url: file,
            success: function(data)
            {
                var lines = data.split("n"); // Get All Lines
                var currentLine = lines.length;
                currentLine = currentLine - 2;
                currentLine = lines[currentLine];
                currentLine = currentLine.split(":");

                if(currentLine.length == 4)
                {
                    $(document).prop('title', currentLine[0]);
                }
                if(currentLine.length == 5)
                {
                    $(document).prop('title', currentLine[4]);
                }
                for(var i = 0; i < (lines.length - 1); i++)
                {
                    var LineContent = lines[i].split(":"); // Split the current line by delimiter
                    var newLine = $("<div class="newLine">" + LineContent[1] + ": " + LineContent[3] + " (" + LineContent[2] + ")</div>"); // Set up the new div
                    $("body").prepend(newLine); // Prepend the new line so the newest line is at the top
                }
            },
            dataType: 'text'
        });
    }, 1000);
    });

您没有启动超时。为了使其工作,请将其放置在函数中,然后在超时内调用该函数。像这样:

    function start() {
        setTimeout(function() {
            console.log('it was ran!');
            $.ajax({
                url: file,
                dataType: 'text',
                success: function(data) {
                    // ....your other code
                    start(); //starts the loop again
                }
            });
        }, 1000);
    }
   start();//runs the function at load

最新更新