-1

I am trying to read a text file over and over again, and then change the page based on the text. the way I am currently reading the files is taking to long, and I am asking if there is a faster way of doing it.

<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>

all files are one line long and less than 10 characters each.

Tyler Silva
  • 411
  • 6
  • 14
  • Not really answering your question, but is there a reason you can't convert the files to json and use an ajax request to grab the json data? – Michael Goodwin Feb 03 '17 at 23:29
  • Also not really answering your question but is there not errors in the console doing this as its an odd datatype - perhaps add $.get('showscore.txt', function(data) {...}, 'text'); – dmoo Feb 03 '17 at 23:34
  • Just a note: your score1 stores it's result in score2 :).. Like @Michael say, storing the data in a simple JSON, and reading that would be nicer. But even so, I can't see why reading 5 text files is taking a long time, you running your website over a 300 baud modem. :) – Keith Feb 03 '17 at 23:35
  • You should really clean up your code and read up on ajax calls. `while(true)` will always run, forever! Is that really what you want and in that while loop setting a timeout to wait for results? Seems to me you should really start over and learn the basics first. – Michelangelo Feb 03 '17 at 23:38
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Michelangelo Feb 03 '17 at 23:40

1 Answers1

1

Dude! You're creating an infinite loop, that's why it takes too long.

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!
}

You're requesting the files and setting the timeout dozens or houndreds of times a second.

Do something like this instead:

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");
}

If its still slow, it's your server. I'd recommend creating a single file with all the data (maybe JSON or XML) and making only one request each time. I strongly suggest you to do that.

Gabriel
  • 2,170
  • 1
  • 17
  • 20
  • it needs to continually keep checking, very quickly, because it is going to be used to apply affects to a livestream, in real time. im figuring out how to do it, before putting it in the actual site – Tyler Silva Feb 04 '17 at 16:13
  • Just use a smaller interval in the `timeOut`. But you have to get rid of the loop and wait the requests to finish before making new ones, otherwise you're making the client's browser too slow or even halt and your server will seize, it may even ban the client's IPs. And you won't know which request finished first. You may get an update from the last request, then from the first one, then the third one finishes, and so on. Fetching everything in a single request will help, too. Maybe you could merge everything in a single file on the fly with PHP or other server side script. – Gabriel Feb 04 '17 at 17:02