0

So I am making a site that shows the value of a .TXT file, this is what I got so far:

<script>
$(document).ready(function() {
 $("#responsecontainer").load("info.txt");

var refreshId = setInterval(function() {
  $("#responsecontainer").load('info.txt');
 }, 1000);
 $.ajaxSetup({ cache: false });
});
</script>

So it get's the string from info.txt and puts it inside the div resonsecontainer.

But now I want that same string to be in a value of a meter, and also a auto updater for that. The code is:

<meter id="cpumeter" value="60" min="0" max="120.47" title="GB">
<div class="meter-gauge">
<span style="width: 46.42%;"></span>
</div>
</meter>  

How do I autoupdate the value="60" to the string of the info.txt?

Thanks!

2
  • I don't understand what you're asking - are you trying to change the span width? Or change the .TXT file? Or? What is the "meter"? Do you mean a progress bar? Commented Jul 17, 2015 at 0:02
  • I am trying to put the string of info.txt in the value="here" from meter Commented Jul 17, 2015 at 0:15

1 Answer 1

1

You can give a callback argument to .load(), and it will receive the contents of the file.

$(document).ready(function() {
    function loadInfo() {
        $("#responsecontainer").load('info.txt', function(info) {
            $("#cpumeter").val(parseFloat(info));
        });
    }
    $.ajaxSetup({ cache: false });
    loadInfo();
    var refreshId = setInterval(loadInfo, 1000);
});
Sign up to request clarification or add additional context in comments.

10 Comments

Sorry, it was a typo, loadinfo should have been loadInfo in the setInterval
I've added a call to parseFloat in case there's extra whitespace in the file.
Well, the way you wrote the question implied that the contents of the file would be a proper value for the value= attribute.
If the file contains a percentage, it's probably not the right thing to put in value="", because the range of your meter is 0 to 120.47, not 0 to 100.
You probably need to do .val(parseFloat(info)/100 * 120.47)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.