Tip: Click lines to highlight, hold ctrl/cmd to multi-select
DateJS throttling (21-Jun @ 13:46)
Syntax Highlighted Code
- function delay(fn, time) {
- return function () {
- var t = this;
- if (fn.timer) {
- clearTimeout(fn.timer);
- }
- fn.timer = setTimeout(function () {
- fn.apply(t);
- }, time);
- };
- }
- $(function () {
- var humanDate = $('#human_date').keyup(delay(function () {
- if (humanDate.data('prevValue') == humanDate.val()) return;
- humanDate.data('prevValue', humanDate.val());
- var date = '';
- try {
- date = Date.parse(this.value).toString("d-MMMM yyyy"); // note different formats available
- } catch (e) {
- date = 'Sorry, I cant do that date';
- }
- $('#info').html(''); // error notice field
- $('#actualDate').text(date); // date selected (i.e. in plain text)
- }, 200));
- // force a trigger
- humanDate.keyup();
- });
Plain Code
function delay(fn, time) {
return function () {
var t = this;
if (fn.timer) {
clearTimeout(fn.timer);
}
fn.timer = setTimeout(function () {
fn.apply(t);
}, time);
};
}
$(function () {
var humanDate = $('#human_date').keyup(delay(function () {
if (humanDate.data('prevValue') == humanDate.val()) return;
humanDate.data('prevValue', humanDate.val());
var date = '';
try {
date = Date.parse(this.value).toString("d-MMMM yyyy"); // note different formats available
} catch (e) {
date = 'Sorry, I cant do that date';
}
$('#info').html(''); // error notice field
$('#actualDate').text(date); // date selected (i.e. in plain text)
}, 200));
// force a trigger
humanDate.keyup();
});