1

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);

1 Answer 1

1

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".

Sign up to request clarification or add additional context in comments.

4 Comments

shouldn't it be set on ready? otherwise there might be a problem if jQuery was delayed in loading...
It doesnt seem to work. This is the code im trying now: var tempUpdate=function() { $('#curtemp').load('grabsensorinfo.php') }; setInterval(tempUpdate,5000); damn editing ......
code edited as suggested by Moak, try it now. Also, have you checked using the console that the resource you want to load is correctly returned?
Im trying it now! The php script works by itself when I call it seperatly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.