调用webservice asynch正在推迟渲染网页

时间:2015-05-31 17:58:12

标签: jquery

我正在打开打开天气api的异步电话,但网站其余部分的呈现正在等待回复。它不应该被异步调用停止。

 function getWeather(coords, callback) {
  $("#lw").val("Retrieving weather report ...");
    var url = 'http://api.openweathermap.org/data/2.5/weather';
    $.ajax({
      dataType: "jsonp",
      url: url,
      timeout: 800,
      async: true,
      jsonCallback: 'jsonp',
      data: { lat: coords[0], lon: coords[1],units:'metric',cnt:1},
      cache: true,
      success: function (data) {
        callback(data);
      }
    });
  }

      var places = [
        {
          places: "Blatten",
          Long: 7.8194,
          Lat: 46.4205,
          Weather: "api.openweathermap.org/data/2.5/weather?lat=46.4205&lon=7.8194&units=metric&mode=json"
        }
      ];

        for (var place in places) {
          var obj = places[place];
          (function (place) {
            coords = [place.Lat, place.Long]
            getWeather(coords, function(data) {
              var html = [];
              html.push('<div>')

var itemp =data.main.temp;
itemp = itemp.toFixed(1);

var itemp_min =data.main.temp_min;
itemp_min = itemp_min.toFixed(1);

var itemp_max =data.main.temp_max;
itemp_max = itemp_max.toFixed(1);

var windNames = new Array("North","North Northeast","Northeast","East Northeast","East","East Southeast", "Southeast", "South Southeast","South","South Southwest","Southwest","West Southwest","West","West Northwest","Northwest","North Northwest");
var windShortNames  = new Array("N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW");
var wnd  = windNames[Math.round((data.wind.deg -11.25) / 22.5)];
var src = '/wetter/images/bg/' + data.weather[0].icon + '.png';

    html.push('<p>Current conditions ', data.name, ' at low altitude: ');
    html.push(' ', itemp + '  &#8451;', '  ');
    html.push(' (min ', itemp_min + '  &#8451;', ' / ');
    html.push(' max ', itemp_max + '  &#8451;', '), ');
    html.push(' ', data.weather[0].description);
    html.push(', cloud cover ', data.clouds.all, '%. ');
    html.push('</div>')

            });
          }(obj));
        }  

0 个答案:

没有答案