I want to get a simple data (a number in fact) from a php file (via ajax or json) into a vitiable in jQuery.
I have tried a lot of Ajax and jSon method... no success.
I got this:
while (data.length < totalPoints)
{
var prev = data.length > 0 ? data[data.length - 1] : 60;
var y = prev + Math.random() * 10 - 5;
if (y < 0)
y = 0;
if (y > 60)
y = 60;
data.push(y);
}
I want to replace the "Math.random() * 10 - 5" with a number in a php file that have a simple number in it from MySql.
This loop will go on 60 times for the first time an only once every 15 sec.
This is not working. The php file is call and return the number but the 'fromPhp' is empty.
var data = [], totalPoints = 60;
function getRandomData() {
if (data.length > 0)
data = data.slice(1);
var fromPhp = '';
var y;
while (data.length < totalPoints) {
var prev = data.length > 0 ? data[data.length - 1] : 60;
$.get('assets/get_number.php', function (theNumber) {
fromPhp = theNumber;
});
y = prev + fromPhp;
if (y < 0)
y = 0;
if (y > 60)
y = 60;
data.push(y);
}
var res = [];
for (var i = 0; i < data.length; ++i)
res.push([i, data[i]])
return res;
}
Actually all the number (y) are '60'