I have a postcode field that has a jQuery onKeyup event - the idea is that once they have fully entered their postcode to call an Google Maps Geocoding API to get the location immediately based on this postcode.
This code works however i'd like to find a solution that will ideally not call the API multiple times but wait and see if the user has finished typing using some method of waiting for x amount of time and then calling the API.
Can anyone suggest the best way to do this?
$("#txtPostcode").keyup(function() {
var postcode = $('#txtPostcode').val().length
if (postcode.length >= 5 && postcode.length <= 8) {
console.log('length is a valid UK length for a postcode');
// some logic here to run with some way to work out if user has 'finished' typing
callGoogleGeocodingAPI(postcode);
}
});