Javascript没有调用函数

时间:2014-05-14 12:36:33

标签: javascript html ajax

调用onLoad调用的函数,但在调用另一个函数之前停止。 我确定这是正在发生的事情,因为我添加了一些调试document.write()函数。

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function getTemp(){
    document.write("3");//debugging
    $.ajax( {
        type: "GET",
        url: "gettemp.php",
        success:function(data){
            document.write(data);
            document.write("4"); //debugging
        }
    });
}

</script>
</head>
<body onload="onLoad();">
<script>
function onLoad(){
    document.write("1"); //debugging
    getTemp();
    document.write("2"); //debugging
    setInterval(function(){getTemp();}, 10000);
}
</script>
</body>
</html>

它只输出“1”,没有别的。因此,我确信在调用getTemp()之前它会停止。如果有必要,我将添加php文件。至于拆分脚本,如果你将onLoad()脚本放在头部脚本中,你最终什么都没有,甚至不是“1”。

编辑:现在它变为“132”,但它似乎没有正确调用PHP。

PHP来源:

<?php
getTemp();
function getTemp()
{
        $filename = "temp";
        $f = fopen($filename,"r");
        $value = fgets($f);
        $value = $value / 100;
        echo $value;
}
?>

1 个答案:

答案 0 :(得分:6)

看起来像拼写错误

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function getTemp(){
        document.write("3");//debugging
        $.ajax( {
            type: "GET",
            url: "gettemp.php",
            success:function(data){
                document.write(data);
                document.write("4"); //debugging
            }
        });
    }
// } <-- not needed

</script>
</head>
<body onload="onLoad();">
<script>
function onLoad(){
    document.write("1"); //debugging
    getTemp();
    document.write("2"); //debugging
    setInterval(function(){getTemp();}, 10000);
}
</script>
</body>
</html>

我使用console.log('foobar')进行调试:)

修改

它正确地调用了php ...

你的问题是你的php源...你总是叫同一行...

你的funaction调用getTemp并且getTemp总是读取第一行......

查看 fgets功能

的示例

<强> SEE SAMPLE FOR fgets

缺少一段时间或任何类型的循环和文件长度参数:D