jquery setInterval和setTimeout不工作,不循环

时间:2015-02-27 22:06:09

标签: jquery settimeout setinterval

我一直遇到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);

    });

1 个答案:

答案 0 :(得分:0)

您没有开始超时。为了使其工作,将其置于函数内,然后在超时内调用该函数。像这样:

    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