Need some help for a "realtime flipcounter"

Hello,

I need some help to modify a piece of code. I found this masterpiece flip counter code on github, and at this moment the script will scrape the kickstarter page for the total $ backed and updates the flip counter at every 5s.

I want to make a little change, to not scrape the kickstarter page but instead to read a small “counter.txt” file located in the same folder with the script, and get the values from that small .txt file.

The flipcounter comes in 2 php files: index.php, scrapet.php;

index.php:

<div id="flipcounter" style="text-align: center;">8531564</div>
<script type="text/javascript">
    // Load sounds
    var soundIndex = 0;
    var sounds = new Array();
    for (i=0; i<10; i=i+1)
        sounds[i] = new Audio("tick.wav");

    // Make the flip counter
    $("#flipcounter").flipCounterInit({
        'speed': 0.05,
        'onFlip': function () { 
            if (soundsEnabled)
            {
                sounds[soundIndex].play();
                soundIndex = soundIndex + 1;
                if (soundIndex >= sounds.length)
                    soundIndex = 0;
            }
        }
    });

    // Only play sounds when window has focus
    var soundsEnabled = true;
    $(window).focus(function() { soundsEnabled = true; });
    $(window).blur(function() { soundsEnabled = false; });

    // Update the counter each 5sec
    function updateLoop() {
        $.ajax({
            url: 'scraper.php',
            data: '',
            dataType: 'json',
            success: function(data) {
                console.log('Updating users to: ' + data.total_users);
                $("#flipcounter").flipCounterUpdate(data.total_users);
                
                window.setTimeout(function () {
                    updateLoop();
                }, 5000);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log('Failed: ' + jqXHR + ' / ' + textStatus + ' / ' + errorThrown);
                window.setTimeout(function () {
                    updateLoop();
                }, 5000);
            },
        });
    }

    // do it!
    updateLoop();
</script>

scraper.php:

<?php

$content = file_get_contents(“http://www.kickstarter.com/projects/ouya/ouya-a-new-kind-of-video-game-console”);

$total = substr($content, strpos($content, “">$”)+3, 15);
$total = substr($total, 0, strpos($total, ‘<’));
$total = str_replace(‘,’, ‘’, $total);

$return = array(‘total_users’=>$total);
echo json_encode($return);

exit;
?>

So, all i want is to read a simple counter.txt file instead of scraping the kickstarter page.

Thank you.

Hi Norbert,

How’s it going?

Obviously your JavaScript is expecting the PHP script to return some JSON:

echo json_encode($return);

It would be helpful to see what exactly it is expecting. Could you then alter your success callback like so and let us know what gets logged to the console.

success: function(data) {
  console.log(data);    <---- Add this

  $("#flipcounter").flipCounterUpdate(data.total_users);
   window.setTimeout(function () {
    updateLoop();
  }, 5000);
},

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.