Reputation: 395
I have my main file which is displaying some graphs etc - this works fine.
Now I came up with the idea to display some temperatures in a more timely fashion - as in - refresh it every like 2 - 10s.
Ive added a little php script which explodes the sensor info and formats it, this works fine by itself:
$sensor1 = file_get_contents('sensor1');
$sensorinfo1 = explode ("t=", $sensor1);
$sensor2 = file_get_contents('sensor2');
$sensorinfo2 = explode ("t=", $sensor2);
printf("Aktuelle Temperaturen: Sensor1: %s Sensor2: %s", $sensorinfo1[1] / 1000,$sensorinfo2[1] / 1000);
My knowledge about Javascript, Ajax and jQuery is rather ... well. Lets not talk about it. I came up with the following things for that part after searching around for a while already (it doesnt work, and I do not know why):
setInterval(function () {
$('#curtemp').load('grabsensorinfo.php', function () {
}, 5000);
});
and the div element:
<div class="curtemp">
</div>
So what I'd like to have is the div element being updated every X seconds.. Anyone got an idea where I go wrong?
This is what I got to now - not working still:
var tempUpdate=function() {
$('#curtemp').load('grabsensorinfo.php')
};
setInterval(tempUpdate,5000);
Upvotes: 1
Views: 2942
Reputation: 3507
You have to call the function without the ()
, this way:
var callme=function () {
$('#curtemp').load('grabsensorinfo.php');
};
EDIT: you don't need to pass an empty function when calling load()
, just call it with the page address as an URL. Try it inside Chrome of Firefox console to check that it works correctly
then (after loading the page as suggested):
$(function(){setInterval(callme,5000);});
otherwise, JavaScript will call the function once at the beginning, and then call the returned value.
It seems strange, let's say that now you are saying "please, call the returned value of this function every 5000 ms", not "call this function every 5000 ms".
Upvotes: 1