To many queries in a minute somehow

  • 1
  • Question
  • Updated 5 years ago
I have a webapp that get's the geo-location and uses the Weather Underground API to get the weather information.
Somehow it calls the API a completely random amount of times, from 3 till sometimes more than 50 times in a minute.
I try to control this, but I don't know what is going wrong here.

I use the Yii framework that calls the javascript function getLocation on pageload.

Yii::app()->clientScript->registerScript('getLocation',"document.ready(getLocation());" ,CClientScript::POS_HEAD);


This function should max. get the location 4 times in a minute and run the function getLocationDetails.

function getLocation() {
if(navigator.geolocation){
// maximumAge at 60000 (1 minutes) - Get new location if the cached location is older then maximumAge
// timeout at 15000 milliseconds (15 seconds) - Timeout between new location calls
var options = {enableHighAccuracy:true, maximumAge:600000, timeout:15000};
geoLocation = navigator.geolocation;
watchID = geoLocation.watchPosition( getLocationDetails, errorHandler, options );
}else{
alert("Sorry, browser does not support geolocation!");
}
}


The function getLocationDetails (ajax to run serverside) then calls the Weather Underground API that get's the weather-information and fill the formfields.

function getLocalWeather(position) {
// Serverside call to parse data from Wunderground
jQuery(document).ready(function($) {
$.ajax({
url : "http://api.wunderground.com/api/<MYID>/geolookup/conditions/lang:NL/q/"+position.coords.latitude.toFixed(7)+","+position.coords.longitude.toFixed(8)+".json",
dataType : "jsonp",
success : function(parsed_json) {
document.getElementById("Waarnemingen_condition").value = parsed_json['current_observation']['weather'];
document.getElementById("Waarnemingen_temp").value = parsed_json['current_observation']['temp_c'];
document.getElementById("Waarnemingen_humidity").value = parsed_json['current_observation']['relative_humidity'];
document.getElementById("Waarnemingen_icon").value = parsed_json['current_observation']['icon'];
document.getElementById("Waarnemingen_wind_degrees").value = parsed_json['current_observation']['wind_degrees'];
document.getElementById("Waarnemingen_wind_kph").value = parsed_json['current_observation']['wind_kph'];
document.getElementById("Waarnemingen_wind_gust_kph").value = parsed_json['current_observation']['wind_gust_kph'];
document.getElementById("Waarnemingen_pressure").value = parsed_json['current_observation']['pressure_mb'];
document.getElementById("Waarnemingen_pressure_trend").value = parsed_json['current_observation']['pressure_trend'];
document.getElementById("Waarnemingen_dewpoint_c").value = parsed_json['current_observation']['dewpoint_c'];
document.getElementById("Waarnemingen_feelslike_c").value = parsed_json['current_observation']['feelslike_c'];
document.getElementById("Waarnemingen_visibility_km").value = parsed_json['current_observation']['visibility_km'];
},
error: function (response, status) { alert("Error!"); }
});
});
}


How can this run in a random amount of times?

I hope it is possible to fix this.
Problem now is that when it calls the API more then 10 times in a minute I can not use the API for the hole day. Instead of just a max of 10 calls in one minute.
Photo of Ronald de Vries

Ronald de Vries

  • 4 Posts
  • 0 Reply Likes

Posted 5 years ago

  • 1

Be the first to post a reply!