0

How can I get a variable in my javascript function to update every time the script is ran? The variable grabs a PHP string, which it then seems it stores it and doesn't update the value ever till a page refresh is done.

SCRIPT:

setInterval(function () {
        var options = {
            useEasing : true, 
            useGrouping : true, 
            separator : ',', 
            decimal : '.', 
        };
        var value123 = <?php echo $amount22; ?>;
        var value234 = <?php echo $unpaidbtc; ?>;
        var demo = new CountUp("counter", value123, value234, 10, 1, options);
        demo.start();
      }, 5000);

Basically it grabs the $unpaidbtc value for my php in the page. This script will run every 5 seconds, but instead of updating with the new value the php has, it uses the first value it grabbed and never updates to the new PHP string.

Is there anyway to make the script update and re grab the PHP script from the page automatically?

Example: $unpaidbtc = 10 on start and $unpaidbtc = 15 after 5 seconds, but counter only goes up to 10. How can it grab the new value without refreshing the page and stuff, thanks!

31
  • 3
    you have to use ajax. Commented Jul 18, 2017 at 0:07
  • 3
    "Never have worked with that" --- now as you have a keyword, spend few minutes and make a research. Commented Jul 18, 2017 at 0:09
  • 1
    @ObsidianAge - That example requires jQuery, though. If it's not already included in the OP's project, there's no need to include it just to make a simple Ajax call. It's literally 8 rows of code if you go with native js... Commented Jul 18, 2017 at 0:12
  • 2
    Even though it sounds trivial - updating a counter - it's not. You have to determine where you want to save this counter. You can save this counter in a cookie/local storage, or you can save this on your server (with a file or database). Saving the counter on the client's computer means when you move to another computer the counter is different. If you save the counter on the server, everyone will see the same counter - but they will also all be able to modify the counter. You could also have different counters for different users. You should probably give us more info about this. Commented Jul 18, 2017 at 0:22
  • 2
    There is no even a single mention of "animation" in the question. Could you update the question so that it addressed the real problem? Commented Jul 18, 2017 at 0:36

1 Answer 1

1

From the comments I think I understand what you want:

var ajax = new XMLHttpRequest();

function getValues(callback) {
    ajax.abort();

    ajax.open("GET", "index.php?showme=1", true);
    ajax.send();

    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4 && ajax.status == 200)
            callback(JSON.parse(ajax.responseText));
    };
}

setInterval(function() {
    var options = {
        useEasing: true,
          useGrouping: true,
          separator: ',',
          decimal: '.',
    };

    getValues(function(obj) {
        var value123 = obj.amount22;
        var value234 = obj.unpaidbtc;
        var demo = new CountUp("counter", value123, value234, 10, 1, options);
        demo.start();
    });

}, 5000);

You can get away with only using one PHP page, let's assume it's called index.php:

<?php

//do your database calls to get $amount22 and $unpaidbtc

if (isset($_GET['showme'])) {

    echo json_encode(array(
        'amount22'  => $amount22,
        'unpaidbtc' => $unpaidbtc
    ));
    exit;
}

//the rest of your page (with the html/javascript)

Note that if your page is not called index.php, you need to change this line:

ajax.open("GET", "index.php?showme=1", true);
Sign up to request clarification or add additional context in comments.

4 Comments

This is what I want, but is there anyway to convert the php string to json from the same file and read it from that file too instead of using that external php file? Could I use # for it?
Yup, you can put everything into one page. I've edited to show how.
The # sign, if I'm understanding correctly, is used only for client sided navigation, i.e page.php#hello - PHP can't read this value. I've used index.php?showme=1 to only show the values in JSON.
Thank you for your time and effort, I have found a way to work this how I needed!

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.