Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting data only runs once

    A minor modification to the http://smoothiecharts.org/ example.
    line2.append(new Date().getTime(), Math.random());
    becomes
    line2.append(new Date().getTime(), updist());

    var x = 0;
    function updist()
    {
    x = ("<?PHP phpread() ?>");
    return (x);
    }


    <?php
    function phpread()
    {
    $t=$x=$y=$z=0;
    $fd = fopen ("/tmp/seis", "r"); // /tmp/seis is a single line that changes 20/sec
    $foo = fscanf ($fd, "%s", &$t);
    fclose($fd);
    $i = explode (",", $t);
    echo "$i[2]"; // should go: 2 7 9 4 0 -1 -8 -4 -5 2....... instead I get 2 2 2 2 2 2 2 2
    }
    ?>
    ----------------
    So "line2.append..." calls updist which calls phpread which works fine - once. It's as if the phpread caches the results of the read and never rereads the file.

    I'm snowed.

    Thanks
    Chuck

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by liderbug View Post
    Code:
    var x = 0;
    function updist()
    {
    x = ("<?PHP phpread() ?>");
    return (x);
    }
    The value of x is set at load time and cannot be updated by repeated calls to the function.
    If you're writing to your file dynamically, presumably without reloading the document, you'll have to read its new contents in a similar way.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, yes and you know that went through my mind - through being the op word...

    Thanks.

  • #4
    New Coder
    Join Date
    Mar 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Now I'm even more confused. So the code sets up var lineN = new TimeSeries(); [ once ]. Then it does setInterval(function() { line1, 2 .append(new Date( ).getTime(), Math.random) or for 2 run a function that pulls in an external number. Math.random re-runs every timeout interval as does updist. Line 1 updates line 2 doesn't. Doesn't TimeSeries in conjunction with setInterval in effect set something up running in the background that triggers ever N miliseconds.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    No - the PHP code runs once at the start on the server before the JavaScript (and the rest of the page is sent to the browser). The JavaScript only runs after the page loads in the browser and after the PHP updates have been done.

    If you want to retrieve multiple values from the server then you either need to do all of that part of the processing in PHP prior to loading the page or you need to use an Ajax call from the JavaScript to run additional PHP scripts after the page has loaded.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •