jquery - calling webservice asynch is delaying rendering webpage -
i making asynch call open weather api, rendering of rest of website waiting response. should not stopped asynch call.
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" } ]; (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, ' @ low altitude: '); html.push(' ', itemp + ' ℃', ' '); html.push(' (min ', itemp_min + ' ℃', ' / '); html.push(' max ', itemp_max + ' ℃', '), '); html.push(' ', data.weather[0].description); html.push(', cloud cover ', data.clouds.all, '%. '); html.push('</div>') }); }(obj)); }
Comments
Post a Comment