setInterval函数使用innerHTML不更新

时间:2014-01-31 07:12:15

标签: javascript function setinterval innerhtml

我似乎无法理解为什么这不会更新输出中显示的内容。我已经为任何人看过了一个小伙伴。

http://jsfiddle.net/sQrn7/

这是代码:

function getPrices(basePrice){
    var dogeValue = 0.005343614; //When changed the output should change.
    var postage = 0.49/dogeValue;
    var sellAmount = (basePrice/dogeValue - postage) - (basePrice*0.1/dogeValue);
    rounded = Math.round(sellAmount);
    document.getElementById("txt").innerHTML = rounded;
}
var timer = setInterval(function() { getPrices(10) }, 1000);

原始/完整代码:

<html>
<script>
var rounded = 0;
    function getPrices(basePrice){
        var dogeValue = <?php echo(file_get_contents("https://www.dogeapi.com/wow/?a=get_current_price")); ?>;
        var postage = 0.49/dogeValue;
        var sellAmount = (basePrice/dogeValue - postage) - (basePrice*0.1/dogeValue);
        rounded = Math.round(sellAmount);
        document.getElementById("txt").innerHTML = rounded;
    }
    var timer = setInterval(function() { getPrices(10) }, 1000);
</script>
<body onload="getPrices(10);">
    <div id="txt"></div>
</body>

2 个答案:

答案 0 :(得分:0)

如果您查看控制台,则会显示Uncaught ReferenceError: sellAmount is not defined

原因:您在变量sellAmount的定义中缺少空格:您写了varsellAmount = ...

使用它,它将起作用:

function getPrices(basePrice){
    var dogeValue = 0.005343614; //When changed the output should change.
    var postage = 0.49/dogeValue;
    var sellAmount = (basePrice/dogeValue - postage) - (basePrice*0.1/dogeValue);
    var rounded = Math.round(sellAmount);
    document.getElementById("txt").innerHTML = rounded;
}
var timer = setInterval(function() { getPrices(10) }, 1000);

在此处查看更新后的Feed:http://jsfiddle.net/sQrn7/

编辑:在您编辑过的问题之后,请看这个小提琴:http://jsfiddle.net/Ne2gN/

输出始终相同,因为dogeValue的值始终保持不变。您必须更改该值,才能看到输出的变化。

这是新代码:

var dogeValue = 0.005343614; //When changed the output should change.
function getPrices(basePrice){

    var postage = 0.49/dogeValue;
    var sellAmount = (basePrice/dogeValue - postage) - (basePrice*0.1/dogeValue);
    var rounded = Math.round(sellAmount);
    document.getElementById("txt").innerHTML = rounded;
    dogeValue+=0.1;
}
var timer = setInterval(function() { getPrices(10) }, 1000);

编辑2:在您的问题编辑之后,答案保持不变 - 您仍然可以全局定义:

<html>
<script>
var rounded = 0;
var dogeValue = <?php echo(file_get_contents("https://www.dogeapi.com/wow/?a=get_current_price")); ?>;

    function getPrices(basePrice){        
        var postage = 0.49/dogeValue;
        var sellAmount = (basePrice/dogeValue - postage) - (basePrice*0.1/dogeValue);
        rounded = Math.round(sellAmount);
        document.getElementById("txt").innerHTML = rounded;
        dogeValue+=0.1; //change it
    }
    var timer = setInterval(function() { getPrices(10) }, 1000);
</script>
<body onload="getPrices(10);">
    <div id="txt"></div>
</body>

你可以在你的代码中随处放置你的php-Tags - 如果它们在我们的函数外部并不重要。此代码与您的代码完全相同,唯一不同的是,getPrices()现在可以更改dogeValue

答案 1 :(得分:0)

PHP部分仅在页面加载时执行一次。看一下源代码(右键单击页面,“显示源代码”),您将看到静态分配的值,因为它是在页面加载到浏览器之前用PHP生成的。请记住,Javascript和PHP在不同的上下文中执行:PHP&gt;服务器端,Javascript&gt;客户端。

帮助您解决问题:http://www.w3schools.com/ajax/