AJAX methods like $.get
are asynchronous, meaning the script doesn't wait for them to finish. The function you passed to $.get
can return something, but it kind of gets dumped into the bit bucket - there's nowhere useful to return it to.
Your code needs a little bit of a redesign to take full advantage of being asynchronous. Change the function inside setInterval
to this:
setInterval(function() {
// current time - adjust to align with data vals
var x = (new Date()).getTime();
// we need to do this in here so `function(result)` can use `x`
$.get('testget.php', function(result) {
// this gets run when the result is received
var y = parseFloat(result);
series.addPoint([x, y], true, true);
});
}, 3000);
The work is now done when $.get
gets its result and invokes its function, which is whenever your PHP runs.
(You might be worried that the async stuff will mean data points might be received out-of-order. That won't happen - you're specifying the x-coordinate manually, and each time the outer function is run a new x
is created. Every y
will be paired with its matching x
.)