簡體   English   中英

你可以有一個以上的ajax嗎?

[英]can you have more than one ajax?

我正在使用ajax從速度較慢的數據庫中提取數據,這最多需要2.5秒。 它成功更新了網頁標簽,但是當我嘗試將其傳遞到Highcharts量表時,毫無疑問,它失敗了。 我以為如果可以更新全局,然后將其傳遞給圖表,它將起作用,但沒有。 我試圖將圖表延遲到ajax完成之前,但失敗了。 我認為問題是我似乎無法更新全局。 我想發布一些代碼,但是上一次我試圖在此站點上放置HTML,結果是錯誤的。

您的西蒙M.

 // JavaScript Document
 // simple ajax to get data
    $(function () 
                {
                $.ajax({
                            url: 'extractLatest.php',   // call php file

                    success: function(datafromphp){ // when it is sucessful
        $( "#latest_mb" ).html(                 // replace html tag <div id="latest_mb"> contents 
                         datafromphp                                    // with whats returned from php
                            );


                                    var position    = "#gauge-chart-a"; // html container name
    // == start of chart == //
        $(function() {
          $(position).highcharts({
              chart: {
                type: 'gauge',
              },
              title: {
               text: 'Last Reading: '+ datafromphp +'mB at '+ msToTime(latest_UTC)
              },

                  pane: {
                startAngle: -150,
                endAngle: 150,
              },
              // the value axis
              yAxis: { // Month
                        // find scale max and min to nearest 10
                min: ((month_lo_mb/10)-0.5).toFixed(0)*10,
                max: ((month_hi_mb/10)+0.5).toFixed(0)*10,
                    title:{text:'mB'}, 

            plotBands: [

                    { // Month
        from: month_lo_mb,
          to: month_hi_mb,
        color: '#9DB6F9'}, // Blue

                    { // Week (Sun thro Sat)
        from: wk_lo_mb,
          to: wk_hi_mb,
        color: '#55BF3B'}, // green

                    { // Day
        from: day_lo_mb,
          to: day_hi_mb,
        color: '#FFFF63'}, // yellow

                    { // Day Avg
                // display day average line in red
        from: day_av_mb-0.125,
          to: day_av_mb+0.125,
        color: '#ff0000'} // red

                ]},
              series: [{ data: [datafromphp] }] // current
            }); // end of chart
        });
    }});
  });

這是我最接近需要的東西

<?php
//------------------------------------------------------------------------- 
// Latest
 $latest_mb = mysqli_fetch_array(mysqli_query($dbconnect, "\n"
  ." SELECT UTC,mB \n"
  ." FROM thundersense  \n"
  ." ORDER BY utc DESC LIMIT 1"));

echo $latest_mb[1];
echo sprintf("<h1>Last reading: %1\$.3fmB<br></h1>",$latest_mb[1]);
echo "<h2><i> taken at " . date("H:i:s",$latest_mb[0]) . "</i></h2>";
?>

返回:

  < !--
  Start of data extraction
  -- >


982.976

正如我所想,這是我缺乏經驗的Ajax代碼段

     success: function(datafromphp){
     latest_array = JSON.parse(datafromphp);
     );

並添加到高圖...

    series: [{ data: [latest_array[1]] }]

PHP返回

echo json_encode($return,JSON_NUMERIC_CHECK);

現在可以了。 只需破解定期更新即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM