使用JS从JSON推送的Round值

时间:2014-07-25 07:56:47

标签: javascript jquery json rounding

我绝对是JS的新手,我无法想象我应该如何处理来自外部文件IOCounter.html和Json-Structure的值(Counter1& Counter2)。

我试着将math.round()放到代码中的不同位置,这似乎对我来说是合乎逻辑的 - 但没有任何效果。

我是否必须定义Var's,对这些进行舍入并将结果写入标签?

感谢您的帮助。

 <script src="jquery-2.1.1.min.js"></script>
 </head>
 <body>
  <label id="counter1">0</label>
  <label id="counter2">0</label>
 </body>

 <script type="text/javascript">
    $(document).ready(function(){
        $.ajaxSetup({ cache: false });
        setInterval(function() {
            $.getJSON("IOCounter.htm", function(result) {
                $('#counter1').text(result["counter1"]);
                $('#counter2').text(result["counter2"]);

            });
        },50);
    });

    </script>

1 个答案:

答案 0 :(得分:0)

您不必定义变量本身。你可以这样:

$('#counter1').text(Math.round(result["counter1"]));