...

View Full Version : Getting data only runs once



liderbug
03-08-2012, 12:24 AM
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. :confused:

Thanks
Chuck

Logic Ali
03-08-2012, 12:37 AM
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.

liderbug
03-08-2012, 12:45 AM
Ah, yes :o and you know that went through my mind - through being the op word...

Thanks.

liderbug
03-08-2012, 01:43 AM
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.

felgall
03-08-2012, 02:14 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum