【问题标题】:Analog meter jquery with php data带有 php 数据的模拟仪表 jquery
【发布时间】:2013-09-16 11:38:46
【问题描述】:

我在 Jquery 中有一个模拟仪表的示例代码:

 <script>
    var g1;
    window.onload = function(){

        var g1 = new JustGage({
          id: "g1", 
          value: getRandomInt(0, 100), 
          min: 0,
          max: 100,
          title: "Big Fella",
          label: "pounds"
        });

        setInterval(function() {                
            g1.refresh(getRandomInt(50, 500));
        }, 2500);
    };
</script>

<div id="g1"></div>

现在,我正在尝试根据我的数据库信息更改此值。

有什么想法吗?

【问题讨论】:

    标签: javascript jquery raphael graphael justgage


    【解决方案1】:

    而不是:

    setInterval(function() {
       g1.refresh(getRandomInt(50, 500));
    }, 2500);
    

    您必须向服务器调用 Ajax 请求并将返回的数据推送到模拟仪表:

    setInterval(function() {
       $.ajax({
          url : '<server address to call>',
          type:'post',
          dataType : 'json',
          success : function(response){
            g1.refresh(getRandomInt(response.value1, response.value2));
          }
      });
    }, 2500);
    

    现在,当调用服务器时,您可以对数据库的数据进行任何操作,并以 JSON 格式返回数据。

    但请确保您从服务器返回 JSON,并且也使用两个键 "value1""value2"

    【讨论】:

    • 我的函数是:setInterval(function() { $.ajax({url:"analog_meter_data.php",success:function(result){ g1.refresh(result); }}); } , 2500);但不起作用:x
    • 尝试首先在回调中打印响应: setInterval(function(){ $.ajax({ url:"analog_meter_data.php", success:function(result){ //看看是什么来这里 (Ctrl+Shift+J) console.log(result); g1.refresh(result); } }); }, 2500);
    【解决方案2】:

    使用 jquery ----

    假设 ajaxurl 给出当前值的结果,那么你可以这样使用

     setInterval(function() {    
        $.ajax({url:"ajaxurl",success:function(result){
          g1.refresh(result);
        }});
     }, 2500);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      • 2019-07-16
      • 2013-06-14
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多