doy2255 2013-03-18 16:08
浏览 43
已采纳

PHP / jquery / ajax在设定的时间间隔内加载小的PHP脚本

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条回答 默认 最新

  • dongmaoxi0477 2013-03-18 16:13
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题