jquery快速读取文件

时间:2017-02-03 23:26:10

标签: javascript jquery html

我正在尝试一遍又一遍地阅读文本文件,然后根据文本更改页面。我目前正在阅读文件的方式很长,我想问是否有更快的方法。

<script>
scoreIsShown = false
team1 = ""
team2 = ""
score1 = 0
score2 = 0
function UrlExists(url)
{
    $.get( "showscore.txt", function(data) {
        $(".result").html(data);
        showscore = (data.toLowerCase() == "true")

    });
    $.get( "team1.txt", function(data) {
        `enter code here`$(".result").html(data);
        //(data);
        team1 = data

    });
    $.get( "team2.txt", function(data) {
        $(".result").html(data);
        //(data);
        team2 = data

    });
    $.get( "score1.txt", function(data) {
        $(".result").html(data);
        //(data);
        s = (data.toLowerCase() == "true")
        score2 = parseInt(data)

    });
    $.get( "score2.txt", function(data) {
        $(".result").html(data);
        //(data);
        score2 = parseInt(data)

    });

}
function showScore1()
{
    x = document.createElement("table")
    tr = document.createElement("tr")
    td = document.createElement("td")
    team1p = document.createTextNode(team1)
    td.appendChild(team1p)
    tr.appendChild(td)
    x.appendChild(tr)
    document.body.appendChild(x)
}
function onload()
{
    while (true){
        UrlExists("binary1.txt")
        setTimeout(function()
        {
            if (showscore)
            {
                if (!scoreIsShown)
                {
                    showScore1()

                }
            }else{
                if (scoreIsShown)
                {
                    //hideScore()
                }
            }
        }, 10)
    }
}
</script>

所有文件都是一行长,每行少于10个字符。

1 个答案:

答案 0 :(得分:1)

多德!您正在创建一个无限循环,这就是花费太长时间的原因

while (true){
    UrlExists("binary1.txt") //<--Everything here is async so it returns immediately
    setTimeout(function()
    {
        if (showscore)
        {
            if (!scoreIsShown)
            {
                showScore1()

            }
        }else{
            if (scoreIsShown)
            {
                //hideScore()
            }
        }
    }, 10) //<--This is also async, so it returns immediately
    //You've done almost nothing, lets do it again!
}

您要求提供文件并将秒数设置为每秒几十次或多次。

做这样的事情:

function UrlExists(url) {
    $.when( //start a promise to keep track of every request

        $.get( "showscore.txt", function(data) {
            $(".result").html(data);
            showscore = (data.toLowerCase() == "true")
        }),
        $.get( "team1.txt", function(data) {
            $(".result").html(data);
            //(data);
            team1 = data
        }),
        $.get( "team2.txt", function(data) {
            $(".result").html(data);
            //(data);
            team2 = data
        }),
        $.get( "score1.txt", function(data) {
            $(".result").html(data);
            //(data);
            s = (data.toLowerCase() == "true")
            score2 = parseInt(data)
        }),
        $.get( "score2.txt", function(data) {
            $(".result").html(data);
            //(data);
            score2 = parseInt(data)
        })

    ).done(function() {
        //when all done, continue
        //do the thing you was doing in the while
        if (showscore)
        {
            if (!scoreIsShown)
            {
                showScore1()
            }
        }else{
            if (scoreIsShown)
            {
                //hideScore()
            }
        }
        //and set a timer to call the funcion again later
        setTimeout(function() {
            UrlExists(url);
        },60000);
    });

}

function onload() {
   UrlExists("binary1.txt");
}

如果它仍然很慢,它就是你的服务器。我建议创建一个包含所有数据(可能是JSON或XML)的单个文件,并且每次只生成一个请求。我强烈建议你这样做。