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

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?