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);
}
});
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);
}
});
Share
Improve this question
edited Jan 11, 2016 at 10:14
Rory McCrossan
338k41 gold badges320 silver badges351 bronze badges
asked Jan 11, 2016 at 10:08
ZabsZabs
14.1k50 gold badges179 silver badges311 bronze badges
1
- Possible duplicate of Debounce function in jQuery – Piskvor left the building Commented Jan 11, 2016 at 10:15
3 Answers
Reset to default 9You can use setTimeout
to only make the call after typing has stopped for 250ms - this is generally enough time between keystrokes to allow the full entry. Try this:
var timer;
$("#txtPostcode").keyup(function() {
clearTimeout(timer);
timer = setTimeout(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);
}
}, 250);
});
You can tweak the exact timeout to better suit your needs if you feel there is too much of a delay.
You can also try using .blur() instead of .keyup() in your code, if you haven't tried already.
Here is a functional decorator that will delay the event until the last keypress. You can play with the delay time to get the best feel. 200ms is an arbitrary value.
$("#txtPostcode").keyup(delayEvent( function( e ) {
console.log( 'event fired' );
// this refers to the element clicked, and there is an issue with in the if statement
// you are checking postcode.length.length which probably throws an error.
var postcode = $(this).val();
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);
}
}, 200));
// this is a functional decorator, that curries the delay and callback function
// returning the actual event function that is run by the keyup handler
function delayEvent( fn, delay ) {
var timer = null;
// this is the actual function that gets run.
return function(e) {
var self = this;
// if the timeout exists clear it
timer && clearTimeout(timer);
// set a new timout
timer = setTimeout(function() {
return fn.call(self, e);
}, delay || 200);
}
}
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtPostcode">