I found this function to delay a keyup callback, which works perfectly after the 3 seconds I have set it too.
function delay_callback(callback, ms) {
var timer = 0;
return function() {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
callback.apply(context, args);
}, ms || 0);
};
}
$('#postcode').keyup(delay_callback(function (e) {
do_postcode();
validate_fields();
}, 3000));
So I also want to call it on change like this $('#postcode').on('change',... but that in addition to the delay makes it run on change and then 3 seconds later so its running twice.
My "onchange" code
$('#postcode').on('change', function(e) {
e.stopPropagation();
do_postcode();
validate_fields();
});
How can I call the function both on change and after the delay, but not both.
So I want to cancel the delay if the input is changed