dqzpt40064 2015-07-17 10:29
浏览 45
已采纳

特定时间后,Php ajax刷新div

Here i am able to load div value's after a time interval

But I also need to load the div values by default also.(when i the page loads)

How to ?

<script type="text/javascript">

$(document).ready(function() {                               
 setInterval(function(){
     $("#Temperature").html('');
     $("#Humidity").html('');
     $("#Weather").html('');
     $("#Pressure").html('');
     $("#Wind").html('');
     $("#Sunrise").html('');

  $.ajax({
       url: 'api.php',
       dataType: "json",
       type: 'GET',
       success: function(data) {
          if (data) {
            alert('Hi');
            document.getElementById("Temperature").innerHTML = data['temperature'];
            document.getElementById("Humidity").innerHTML = data['humidity'];
            document.getElementById("Weather").innerHTML = data['condition'];
            document.getElementById("Pressure").innerHTML = data['pressure'];
            document.getElementById("Wind").innerHTML = data['wind'];
            document.getElementById("Sunrise").innerHTML = data['sunrise'];
          }

       }
    });
  }, 600000);
 });

</script>


<div id="Temperature"></div>
<div id="Humidity"></div>
<div id="Weather"></div>
<div id="Pressure"></div>
<div id="Wind"></div>
<div id="Sunrise"></div>

api.php

This the api.php from where i am trying to load the data

<?php
  include('yahoo_weather_codes.php');
  //error_reporting(0);
    $BASE_URL = "http://query.yahooapis.com/v1/public/yql";
    $yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.placefinder where state="Andhra Pradesh" and city="Amaravathi")'; 
    $yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
    // Make call with cURL
    $session = curl_init($yql_query_url);
    curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
    $json = curl_exec($session);
    // Convert JSON to PHP object
    $phpObj =  json_decode($json);
    //echo '<pre>';print_r($phpObj).'<pre>';
    $fahrenheit = $phpObj->query->results->channel->item->condition->temp;
    $celsius = round(5/9*($fahrenheit-32));


   $yahoo_data = array();
   $yahoo_data['temperature'] = $celsius;
   $yahoo_data['code'] = $phpObj->query->results->channel->item->condition->code;
   $yahoo_data['humidity'] =$phpObj->query->results->channel->atmosphere->humidity;
   $yahoo_data['condition'] =$phpObj->query->results->channel->item->condition->text;
   $yahoo_data['pressure'] =$phpObj->query->results->channel->atmosphere->pressure;
   $yahoo_data['wind'] =$phpObj->query->results->channel->wind->speed;
   $yahoo_data['sunrise'] =$phpObj->query->results->channel->astronomy->sunrise; 
   $yahoo_data['sunset'] =$phpObj->query->results->channel->astronomy->sunset;
   $yahoo_data['date'] =$phpObj->query->results->channel->item->forecast['0']->date;
   $yahoo_data['city'] =$phpObj->query->results->channel->location->city;
   $yahoo_data['country'] =$phpObj->query->results->channel->location->country;
   $yahoo_data['region'] =$phpObj->query->results->channel->location->region; 
   $yahoo_data['key'] = $phpObj->query->results->channel->item->condition->code;

   echo json_encode($yahoo_data);

?> 
  • 写回答

2条回答 默认 最新

  • douqian2524 2015-07-17 10:53
    关注

    Just move your AJAX loading to a separate function which you can use both on document load and in our interval. And since you already using jQuery, you may as well replace cumbersome getElementById with equivalent jQuery selectors:

    <script type="text/javascript">
    $(document).ready(function() {
      function updateData(){
        $("#Temperature").html('');
        $("#Humidity").html('');
        $("#Weather").html('');
        $("#Pressure").html('');
        $("#Wind").html('');
        $("#Sunrise").html('');
    
        $.ajax({
          url: 'api.php',
          dataType: "json",
          type: 'GET',
          success: function(data) {
            if (data) {
              alert('Hi');
              $("#Temperature").html(data['temperature']);
              $("#Humidity").html(data['humidity']);
              $("#Weather").html(data['condition']);
              $("#Pressure").html(data['pressure']);
              $("#Wind").html(data['wind']);
              $("#Sunrise").html(data['sunrise']);
            }
          }
        });
      }
    
      updateData();
    
      setInterval(updateData, 600000);
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了